Over a million developers have joined DZone.

Building an RSS Reader for Android (RIP Google Reader)

DZone's Guide to

Building an RSS Reader for Android (RIP Google Reader)

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

This tutorial will walk through building an RSS reader on the Android platform (focusing on 3.0 + as it will be using Fragments). All the code is available as a complete, working Android app that you can fork/download and fire up straight away on a compatible Android device/emulator. So feel free to go  grab that from GitHub before continuing. 

It is not an unusual requirement for mobile apps these days to be able to consume an RSS feed from another site (or many) to aggregate the content -  Or maybe you just want to build your own Android app now that Google has announced it will be retiring Reader.

Those of you who have worked with RSS in other JVM languages will know that there are plenty of libraries available that can handle parsing RSS - however, because the android platform doesn't actually contain all the core java classes, almost all of the RSS libraries have not been supported.
Fear not though, as Java's SAX parser is available, so with a bit of code we can get a custom RSS parser up and running in no time!
This walk through will cover off the basics of getting an RSS reader app up and running quickly and will also cover some details of Android's fragment system for tablet optimization as well as some things to watch out for (such as changes in the platform that mean network operations cannot be run in the main thread, which requires some tweaking if you have worked on earlier versions).
All the code for the app is also available on our GitHub so feel free to fork that and try loading it up on your Android device/emulator.

Parsing an RSS Feed:

So to get started we will look at parsing the feed - if you have any experience parsing XML using SAX in Java then you will know how easy this is. All we need to do is to tell the parser which XML nodes we care about, and what to do with them.
If  you have never implemented a SAX parser before, there are three primary methods that we will override: 
  • startElement() - This is called by the parser every time a new XML node is found
  • endElement() - This is called by the parser every time an XML node is closed (e.g. </.. )
  • chars() - this is called when characters are found between nodes
public void startElement(String uri, String localName, String qName, Attributes atts) {
  	chars = new StringBuffer();
public void characters(char ch[], int start, int length) {
  	chars.append(new String(ch, start, length));
public void endElement(String uri, String localName, String qName) throws SAXException {
  	if (localName.equalsIgnoreCase("title")){
		} else if (localName.equalsIgnoreCase("description")){
		} else if (localName.equalsIgnoreCase("published")){
		} else if (localName.equalsIgnoreCase("id")){
		} else if (localName.equalsIgnoreCase("author")){
		} else if (localName.equalsIgnoreCase("content")){
		} else if (localName.equalsIgnoreCase("entry")){
	// Check if looking for article, and if article is complete
	if (localName.equalsIgnoreCase("entry")) {
		currentArticle = new Article();
		// Lets check if we've hit our limit on number of articles
		if (articlesAdded >= ARTICLES_LIMIT){
			throw new SAXException();
Because we only really care about capturing data from the leaf nodes, our startElement() method is left empty. The chars() element has to be watched, as there is no guarantee when it will be called (e.g. in a node like hello world  this method might be called several times between the start and end) so every time we will just append the contents to a StringBuffer - that way we can be sure that we will have captured all the data in the node.  By the time the endElement() method is called, we know that we have the contents of the node itself, and we just have to store the data.  At this point, we just quickly knocked up a POJO with the attributes that we wanted to capture - the Strings that we match on are the node names from the ATOM RSS feed (that Blogger uses) - if you are using another feed, just have a quick look at the feed and update the node names appropriately.

Using our Feed in an Android App

So, that was easy right? Once that parser has run through (and you could use that code standalone in any java app really) then you will have a list of Java objects that have the core details about the latest blog posts on the feed (title, author, datecreated, content, etc) - So now lets look at using it in an Android app.

We will assume a basic understanding of the Android SDK and the concept of Fragments, so won't go back to basics with that stuff.

What we will do, is create a basic ListFragment and an RSSService class that we will use to populate the list. In our ListFragment we will simply tell our RSS service to populate the list:
public void onCreate(Bundle savedInstanceState) {
private void refreshList(){
  rssService = new RssService(this);

Simple right?

Let's take a look at what our helpful RSS service is doing for us.

public class RssService extends AsyncTask<String, Void, List<Article>> {
  private ProgressDialog progress;
	private Context context;
	private ArticleListFragment articleListFrag;
	public RssService(ArticleListFragment articleListFragment) {
		context = articleListFragment.getActivity();
		articleListFrag = articleListFragment;
		progress = new ProgressDialog(context);
	protected void onPreExecute() {
		Log.e("ASYNC", "PRE EXECUTE");

The first thing to note is that this class is extending Android's AsyncTask- The reason for this is that since Android 3.0, you are no longer able to perform network operations in the main application thread, so being as our class is going to have to fetch some RSS feeds we are going to have to spin off a new thread.

As you can see, the constructor just sets some context info that we will use later on, and then builds a progress dialogue - this is then displayed in the onPreExecute() method - This lets us show a "loading" spinning disk whilst we fetch the data.

Android's AsyncTask's primary method that handles the actual work that you want to do asynchronously is called "doInBackground()" - In our case, this is simple - we just invoke our SAX RSS parser and fetch our feed data:

protected List<Article> doInBackground(String... urls) {
	String feed = urls[0];
	URL url = null;
	try {
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser sp = spf.newSAXParser();
		XMLReader xr = sp.getXMLReader();
		url = new URL(feed);
		RssHandler rh = new RssHandler();
		xr.parse(new InputSource(url.openStream()));

Finally, we will override the "onPostExecute()" method of the async class to use our newly fetched list to populate our ListFragment.  You note how when we overrode the doInBackground() method earlier we set the return to List of Articles (where Article is my simple POJO containing my RSS blog post info) - well this must correspond to the argument of the "onPostExecute()" method, which looks like this:

protected  void onPostExecute(final List<Article>  articles) {
	articleListFrag.getActivity().runOnUiThread(new Runnable() {
		public void run() {
			for (Article a : articles){
				Log.d("DB", "Searching DB for GUID: " + a.getGuid());
				DbAdapter dba = new DbAdapter(articleListFrag.getActivity());
				Article fetchedArticle = dba.getBlogListing(a.getGuid());
				if (fetchedArticle == null){
					Log.d("DB", "Found entry for first time: " + a.getTitle());
					dba = new DbAdapter(articleListFrag.getActivity());
			ArticleListAdapter adapter = new ArticleListAdapter(articleListFrag.getActivity(), articles);

Actually, all we really needed to do in this method would be pass our new List or articles to the ListFragment and notify it of the change as below:

ArticleListAdapter adapter = new ArticleListAdapter(articleListFrag.getActivity(), articles);

However, in our application we have added a bit more sugar on the app - and we have actually backed the app with a simple DB that records the unique IDs of the posts and tracks whether or not they have been read to provide a nicer highlighting of listed blog posts.

So that's it - there's plenty more you can add to your RSS reader app, such as storing posts for reading later, and supporting multiple feeds/feed types - but feel free to fork the code on GitHub, or just download on to your android device to enjoy all our NerdAbility updates! 

Application running in Andriod emulator
RSS application running in an Andriod  tablet emulator

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!


Published at DZone with permission of Rob Hinds, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}