Total Pageviews

Tuesday, February 15, 2011

RSS reader for Android in less than 300 lines of code

This tutorial is aimed at all developers who want to make a "fully" functional RSS / news feed reader on Android. Building an RSS reader is one of the most common tasks either as part of an App or an App in itself.

This tutorial assumes that you are familiar with setting up a Android development environment and have the SDK, emulator working. If not you can refer to http://developer.android.com/resources/tutorials/hello-world.html . Once you have the SDK working and are looking for some good resources to try your hand at, I highly recommend API Demos which is an app you can find in the samples folder of the SDK. It has some really useful startup code which you can use in your app.

The good part

- Android UI is very useful with its nice and simple widgets. ListViews will be your friend since feeds fit very intuitively into a feed reader app design.

- Background tasks are an effective and highly recommended way to keep your app responsive and lightweight. AsyncTask is the simplest, most elegant and suggested way to design your app with.

The difficult part

- RSS Feed parsing is perhaps the hardest part of building a feed parser. Feeds are available in a variety of formats RSS 1.0, RSS 2.0 , Atom and so on. Life get's harder because we have malformed feeds and non-compliant feeds out there.

If there is one take away from this tutorial it must be that you must simply not attempt to build your own library or RSS feed parser. It will fail miserably out there. Reuse, reuse!

Luckily, to our respite we have a useful parser feed4j which works excellent with feeds. You can either import this library into your app or setup an AppEngine server to parse feeds for you. In the end that's your choice.

Essentially, you can either do parsing on the client or on a server.

Pros of client parsing
- No server deployment
- Maintain one codebase

Pros of sever parsing
- Thin client, very thin - save resources, be responsive and save battery life. Users appreciate that.
- Easy to push updates
- Fast, fast, fast - feed4j is a very intensive so better not run on a phone, tablet.

So now that we have a good idea what we are diving into let's get our hands dirty.