Tuesday, 19 March 2013

Tech: Building an RSS Reader Android App

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



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:



Simple right?

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



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:



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:



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:



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




35 comments:

  1. HELP!!! I've adapted for other site this project, but The Android App doesn't show any info about xml RSS. What should I do? (Exscuse me fo my English, I'm italian)

    ReplyDelete
  2. Please read github issue https://github.com/nerdability/AndroidRssReader/issues/1

    The content in single view is not shown and doesn't seem to be handled, either.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. In that codding
    Android applications the main purpose of RSS Parser class is to parse the rss xml and return RSSFeed object. When user enters a website url this class will do some of tasks like it Parse the html source code and will get rss url.

    ReplyDelete
  5. Hello, I tried to get the project the work but I get the "unable to initiate" error, please check my stackoverflow post: stackoverflow.com/questions/15986055/android-cant-get-github-project-to-work-unable-to-instantiate-activity

    I hope you can help me!

    ReplyDelete
    Replies
    1. I got it to work now, but (just as the sample apk provided) it doesn't even show any posts (android 4.2.2 Nexus 4)

      Delete

  6. Hi,

    Recently I came across some great articles on your site.
    The other day, I was discussing (http://blog.nerdability.com/2013/03/tech-building-rss-reader-android-app.html)with my colleagues and they suggested I submit an article of my own. Your site is just perfect for what I have written!
    Would it be ok to submit the article? It is free of charge, of course!

    Let me know what you think
    Contact me at anelieivanova@gmail.com

    Regards
    Anele Ivanova

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hello, thanks for this post.
    I have been able to get the code work for a lot of feeds except mine:
    http://www.punchng.com/feed

    I keep getting the following error:
    org.apache.harmony.xml.expatparser&parseexception: at line 14, column 2

    PLEASE HELP!!!
    what am i doing wrong?

    ReplyDelete
  9. Hello, thanks for this post.
    I have been able to get the code work for a lot of feeds except mine:
    http://www.punchng.com/feed

    I keep getting the following error on load:
    org.apache.harmony.xml.expatparser&parseexception: at line 14, column 2
    PLEASE HELP!!!
    what am i doing wrong?

    ReplyDelete
  10. Thanks for providing the handy tutorials in ur post.. It is very useful
    Keep posting

    android apps development

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Hello there! First of all thanks for the post, it have been quite useful for this newbie,.... Second I can't see any of my feeds... Can some one point me what I'm doing wrong... My Sample feed url is http://ec2-54-214-202-8.us-west-2.compute.amazonaws.com/GovBids/AvailBids.asmx/GetBids?agency=&category=&location=&title=

    I update the ArticleListFragment.java with my URL...

    private static final String BLOG_URL = "http://ec2-54-214-202-8.us-west-2.compute.amazonaws.com/GovBids/AvailBids.asmx/GetBids?agency=&category=&location=&title=";

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Hi, I have tested the source code on my sdk, but it found 71 problems. Any suggestions?

    ReplyDelete
    Replies
    1. I was running into the same issue, but once I added the android.support.v7.appcompat library to the build path, they all disappeared. Here's how to do that: http://developer.android.com/tools/support-library/setup.html

      Delete
  15. Would it be possible to consume a Podcast RSS feed and allow users to be able to stream AND download the audio?

    ReplyDelete
  16. Well if you go through apple, there are many different kinds to choose from. However if you have a jailbroken iPhone there are many more. I have a jailbroken iPod touch, and I love it!! 
    App on Sale iTunes

    ReplyDelete
  17. Noob question: I downloaded the packed zip file and placed in my workspace. How do I open the entire file in eclipse?

    ReplyDelete
    Replies
    1. File>>import>>android>>existing android..>>browse to location and finish

      Delete
  18. hi, i am testing with your rss reader and using your rss feed works great. But when i use another rss feed it doesn't show the article detail activity, it seems to get stuck on ArticleDetailFragment.java line 43

    if (getArguments().containsKey(Article.KEY)) {

    KEY seems to be NULL

    I am trying to parse feed: http://www.nusport.nl/rss

    Any suggestions?

    btw: happy 2014

    ReplyDelete
  19. i like your project .. thank you

    ReplyDelete
  20. Thanks for the usefull post.
    Unfortunately I have a runtime error:

    03-13 17:43:22.318: E/AndroidRuntime(7732): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nerdability.android/com.nerdability.android.ArticleListActivity}: java.lang.ClassNotFoundException: Didn't find class "com.nerdability.android.ArticleListActivity" on path: DexPathList[[zip file "/data/app/com.nerdability.android-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.nerdability.android-1, /vendor/lib, /system/lib]]

    ReplyDelete
  21. Hey how to get new blog posted when app is closed and show a notification

    ReplyDelete
  22. The article posted was very informative and useful. You people are doing a great job. Keep going.

    Android Apps Development

    ReplyDelete
  23. 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. earn with mobile

    ReplyDelete
  24. This is great, thanks for sharing.

    Picreel

    ReplyDelete
  25. You can find already ready to use libraries here: http://android-arsenal.com/tag/86

    ReplyDelete
  26. any help with multicategory rss in tabbed view pager format other than keeping it under settings, next is how i can use an endless list view other than loading all listview items at a go

    ReplyDelete
  27. Extraordinary blog!I truly love the wonderful way it is simple on my eyes and the data are elegantly composed.I am thinking about how I may be informed at whatever point another post has been made.I have subscribed to your RSS feed which truly ought to do the trap!Have a decent day!
    ~~~~~~~~~~~~~~~~~~~>>
    Super Hero-Mobile Game

    ReplyDelete