测试RSS解析

pull/36/head
mabdc 5 years ago
parent 4ff6ea6653
commit 8d315cb833
  1. 2
      app/src/main/AndroidManifest.xml
  2. 2
      app/src/main/java/io/legado/app/constant/RSSKeywords.kt
  3. 4
      app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt
  4. 93
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt
  5. 18
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  6. 5
      app/src/main/res/layout/activity_read_rss.xml
  7. 1
      app/src/main/res/layout/fragment_rss.xml
  8. 41
      app/src/main/res/layout/item_rss.xml
  9. 5
      app/src/main/res/menu/main_rss.xml

@ -70,7 +70,7 @@
<activity android:name=".ui.audio.AudioPlayActivity" />
<activity android:name=".ui.explore.ExploreShowActivity" />
<activity android:name=".ui.rss.source.manage.RssSourceActivity" />
<activity android:name=".ui.rss.article.RssArticlesActivity"/>
<service android:name=".service.CheckSourceService" />
<service android:name=".service.DownloadService" />
<service android:name=".service.UpdateService" />

@ -5,7 +5,7 @@ object RSSKeywords {
const val RSS_ITEM = "item"
const val RSS_ITEM_TITLE = "title"
const val RSS_ITEM_LINK = "link"
const val RSS_ITEM_AUTHOR = "dc:creator"
const val RSS_ITEM_AUTHOR = "author"
const val RSS_ITEM_CATEGORY = "category"
const val RSS_ITEM_THUMBNAIL = "media:thumbnail"
const val RSS_ITEM_ENCLOSURE = "enclosure"

@ -10,6 +10,7 @@ import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseFragment
import io.legado.app.lib.theme.ATH
import io.legado.app.ui.rss.article.RssArticlesActivity
import io.legado.app.ui.rss.source.manage.RssSourceActivity
import io.legado.app.utils.startActivity
import kotlinx.android.synthetic.main.fragment_rss.*
@ -34,6 +35,9 @@ class RssFragment : BaseFragment(R.layout.fragment_rss),
super.onCompatOptionsItemSelected(item)
when (item.itemId) {
R.id.menu_rss_config -> startActivity<RssSourceActivity>()
R.id.menu_rss_add -> {
startActivity<RssArticlesActivity>()
}
}
}

@ -1,3 +1,94 @@
package io.legado.app.ui.rss.article
class RssArticlesActivity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import androidx.appcompat.app.AppCompatActivity
import io.legado.app.R
import io.legado.app.data.entities.RssArticle
import io.legado.app.model.rss.RssParser
import io.legado.app.ui.rss.read.ReadRssActivity
import kotlinx.android.synthetic.main.item_rss.view.*
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.sdk27.listeners.onItemClick
import java.net.URL
class RssArticlesActivity : AppCompatActivity() {
val zhihu_dayli = "https://rsshub.app/zhihu/daily"
val nytimes = "https://rsshub.app/nytimes/dual"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var articles = mutableListOf<RssArticle>()
val adapter = ArticleAdapter(articles, this)
verticalLayout {
val url = editText {
hint = "请输入RSS地址"
}
fun loadRss(urlString:String){
Thread {
val xml = URL(urlString).readText()
articles = RssParser.parseXML(xml)
runOnUiThread {
adapter.articles = articles
adapter.notifyDataSetChanged()
}
}.start()
}
button("解析") {
onClick {
val urlString = url.text.toString().trim()
if(urlString != ""){
loadRss(urlString)
}
}
}
button("知乎日报") {
onClick {
loadRss(zhihu_dayli)
}
}
button("纽约时报") {
onClick {
loadRss(nytimes)
}
}
listView {
this.adapter = adapter
onItemClick { p0, p1, p2, p3 ->
startActivity<ReadRssActivity>("description" to articles[p2].description)
}
}
}
}
}
class ArticleAdapter(var articles: List<RssArticle>, var context: Context) : BaseAdapter() {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val item_rss= LayoutInflater.from(context).inflate(R.layout.item_rss, null)
val article = articles[position]
item_rss.title.text = article.title
item_rss.pub_date.text = article.pubDate
if(article.author != null && article.author != ""){
item_rss.author.text = article.author
} else{
item_rss.author.text = article.link
}
return item_rss
}
override fun getItem(position: Int): Any {
return articles[position]
}
override fun getItemId(position: Int): Long {
return 1
}
override fun getCount(): Int {
return articles.size
}
}

@ -1,15 +1,15 @@
package io.legado.app.ui.rss.read
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import io.legado.app.R
import io.legado.app.base.BaseActivity
class ReadRssActivity : BaseActivity(R.layout.activity_read_rss) {
override fun onActivityCreated(savedInstanceState: Bundle?) {
import kotlinx.android.synthetic.main.activity_read_rss.*
class ReadRssActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_read_rss)
val s = intent.getStringExtra("description")
webView.loadData("<style>img{max-width:100%}</style>"+s,"text/html","utf-8")
}
}

@ -3,5 +3,8 @@
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

@ -11,7 +11,6 @@
android:layout_height="wrap_content"
app:attachToActivity="false"
app:title="@string/rss" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"

@ -1,7 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="12dp"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content"
app:cardCornerRadius="2dp"
app:cardElevation="8dp">
</LinearLayout>
<LinearLayout
android:layout_margin="8dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:textSize="18sp"
android:text="Title"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/pub_date"
android:textSize="12sp"
android:text="2019-10-01 13:48:00"
android:textStyle="italic"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/author"
android:textSize="16sp"
android:text="Content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</androidx.cardview.widget.CardView>

@ -2,6 +2,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_rss_add"
android:title="@string/add"
android:icon="@drawable/ic_add"
app:showAsAction="always" />
<item
android:id="@+id/menu_rss_config"
android:title="@string/setting"

Loading…
Cancel
Save