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.
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
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!
RSS application running in an Andriod tablet emulator |
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)
ReplyDeletePlease reply fast! Thank you! =)
DeleteIt's a RSS 2.0 create with Wordpress
DeletePlease read github issue https://github.com/nerdability/AndroidRssReader/issues/1
ReplyDeleteThe content in single view is not shown and doesn't seem to be handled, either.
This comment has been removed by the author.
ReplyDeleteIn that codding
ReplyDeleteAndroid 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.
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
ReplyDeleteI hope you can help me!
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
ReplyDeleteHi,
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
This comment has been removed by the author.
ReplyDeleteHello, thanks for this post.
ReplyDeleteI 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?
Hello, thanks for this post.
ReplyDeleteI 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?
Thanks for providing the handy tutorials in ur post.. It is very useful
ReplyDeleteKeep posting
android apps development
This comment has been removed by the author.
ReplyDeleteHello 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=
ReplyDeleteI 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=";
This comment has been removed by the author.
ReplyDeleteHi, I have tested the source code on my sdk, but it found 71 problems. Any suggestions?
ReplyDeleteI 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
DeleteWould it be possible to consume a Podcast RSS feed and allow users to be able to stream AND download the audio?
ReplyDeleteWell 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!!
ReplyDeleteApp on Sale iTunes
Noob question: I downloaded the packed zip file and placed in my workspace. How do I open the entire file in eclipse?
ReplyDeleteFile>>import>>android>>existing android..>>browse to location and finish
Deletehi, 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
ReplyDeleteif (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
i like your project .. thank you
ReplyDeleteThanks for the usefull post.
ReplyDeleteUnfortunately 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]]
Hi Federico,
Deletedid you fix it? i got the same error
Me Too
DeleteHey how to get new blog posted when app is closed and show a notification
ReplyDeleteThe article posted was very informative and useful. You people are doing a great job. Keep going.
ReplyDeleteAndroid Apps Development
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
ReplyDeleteYou can find already ready to use libraries here: http://android-arsenal.com/tag/86
ReplyDeleteany 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
ReplyDeleteExtraordinary 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!
ReplyDelete~~~~~~~~~~~~~~~~~~~>>
Super Hero-Mobile Game
Rssservice is in charge of parsing data and querying the database I think you can part this into two distinctive errand.What is the point of interest of this approach ?At the point when your app has some content stored in database,data can be displayed and user don't need to sit tight for the end of the parsing process(which can take a while).
ReplyDeletegames hidden object
Building as RSS reader on android app has been tough task, but here in this post it make simple by providing code and other processes. Thanks for sharing such valuable post and by providing coding part make it feasible this process.
ReplyDeleteAndroid App Development Atlanta
This comment has been removed by the author.
ReplyDeletegood work Online GED Courses
ReplyDelete
ReplyDeleteYour blog is really useful for me. Thanks for sharing this useful blog..Suppose if anyone interested to learn Android Course in Chennai please visit fita academy which offers best Android Training in Chennai at reasonable cost.
Java is one of the popular technologies with improved job opportunity for hopeful professionals. Java Training in Velachery helps you to study this technology in details.If you are looking for best Java Institutes in Chennai reach Fita academy.
ReplyDeleteThe article posted was very informative and useful.So,I love everything.Android has a few incredible news readers, keeping in mind a hefty portion of them have passed by the wayside now that Google Reader is no more, others have ventured up to take all important focal point. In the event that we needed to pick one that fits the needs of a great many people and packs in a huge amount of valuable highlights, it would need to be the free, cross-stage, cross-gadget synchronizing feedly.Happy Day!!
ReplyDelete~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Visit apps page
Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing. Android training in chennai | Android training chennai |Android course in chennai | Android course chennai
ReplyDeletevery nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing. Phonegap training in chennai | Phonegap training chennai | Phonegap course in chennai | Phonegap course chennai
ReplyDeleteThis comment has been removed by the author.
ReplyDelete
ReplyDeleteHi I am Emi from Chennai. Thanks for sharing the informative post about Java technology. It’s really useful for me to know more about this technology. Recently I did Java Training Chennai at a leading Java Training Institutes in Chennai.
This comment has been removed by the author.
ReplyDeleteyou can try it for android reader ....
ReplyDeleteit's a paid app but you can free download here
Moon reader pro
http://goo.gl/ApTtmD
VERY INFORMATIVE BLOG. THANKS FOR THE POST.
ReplyDeleteiOS Training in Chennai
http://www.traininginchrompet.com/apple-ios-training-in-chennai.aspx
Excellent post, I could really get some help from here! I think apart from this, we need to make sure that we are hiring the best android app developer and he is capable enough and has the right expertise for this matter. Good Luck!
ReplyDelete
ReplyDeleteExcellent post,
soapui Training in Chennai
Thanks for sharing this valuable information to our vision.....
ReplyDeletemsbi Training in Chennai
The mobile phone alone does not make people rude. Polite people learned to turn their ringers off in the concert hall and to speak in muted voices in crowded areas. In Japan, for example, using a mobile phone in a railway car will earn a sharp rebuke from the conductor. Society ultimately learns hay day mod how to accommodate disruptive technology, so we rarely hear phones ring at the movies today.
ReplyDeleteThis is great news for Android app developers because it will allow them to offer apps to use badoo pro apk on a wider array of the company's hardware. This new change is one of Google's major efforts to tackle the problems that have been experienced with fragmentation. Multiple versions of the same app can now be combined into one Android Market listing.
ReplyDeleteMobile phones have become as ubiquitous as wristwatches once were. Men, women and even children are today connected to others from across the world, all thanks to the new blackmart alpha android smartphone technology.
ReplyDeleteThanks for sharing article. The way you have stated everything above is quite awesome. Keep blogging like this. Thanks. verified Perfect money account
ReplyDeleteIlha de Ferro 2 é uma série original Globo Play, desenvolvida pelos Estúdios Globo, criada por Max Mallmann e Adriana Lunardi, com redação final de Mauro Wilson e direção artística de Afonso Poyart
ReplyDeleteGreat guide on building an RSS reader app! In the context of tech innovation, it's fascinating to see how cryptocurrency and blockchain are also transforming industries. Tools like the DBR calculator are essential for anyone diving into crypto investments, offering insights into the potential returns and risks.
ReplyDeleteThis guide on building an RSS reader Android app is incredibly useful for developers! Speaking of intriguing facts, did you know The The 1982 Movie Poltergeist Used Real Skeletons As – tymoff ? It's a wild story from film history
ReplyDelete