Parse RSS feed for Android

。_饼干妹妹 提交于 2019-11-29 11:52:41

Just as CommonsWare mentioned, you should probably use a third party RSS library for this if you want to keep it simple. The newest RSS library in the Android Arsenal is one I made: PkRSS https://github.com/Pkmmte/PkRSS

The API is extremely simple yet flexible.

// Loads your RSS feed in a background thread
PkRSS.with(this).load(url).async();

// After it has finished loading, use this to get the result
List<Article> articleList = PkRSS.with(this).get(url);

Of course, there are a lot more details if you visit the GitHub page for this library.

I'd use a third-party RSS-parsing library. Four are presently listed in the RSS category on the Android Arsenal.

If not, if the RSS file is likely to be large, I would use SAX and not complain. If you are sure that the RSS file will be small, the DOM parser will give you an API that is more in line with what you are seeking, but it tends to be slower and more memory-intensive.

I found the whole SAX thing difficult to look at. I wrote my own parser for RSS, Atom & RDF.

Try MvNewsFeed class in my AndroidWithoutStupid library on GitHub. https://github.com/vsubhash/AndroidWithoutStupid

MvGeneral.startSyncDownload("http://www.example.com/rss.xml", "/mnt/sdcard/rss.xml");
MvNewsFeed oFeed = new MvNewsFeed("/mnt/sdcard/rss.xml");
for (int i = 0; i < oFeed.moMessages.size(); i++) {
  Log.d("YOUR_TAG", oFeed.moMessages.get(i).msMessageTitle);
  Log.d("YOUR_TAG", oFeed.moMessages.get(i).msMessageContent);
  Log.d("YOUR_TAG", oFeed.moMessages.get(i).msMessageLink);
}

For brevity, I have used a synchronous download method. There is a separate class for asynchronous downloads - MvAsyncDownload. Call the constructor with same parameters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!