Android RSS reader application architecture
Building a richly featured RSS reader is a bit of an ambitious project for a brief tutorial, so let's look at what an RSS reader needs to accomplish. Then you'll build out the important elements which are relevant to XML handling and rendering on the Android platform. When you're done, you'll have a functioning RSS reader with some hooks and plans for further extension in the future.
The following sections detail how the Android RSS reader addresses the major requirements of an RSS reader application.
RSS feeds are available from more Internet sites than you can count. The application needs to specify which RSS feed to work with. A fully featured RSS reader includes one or more means of selecting the desired RSS feed. This can include the ability to choose from a number of sites and channels, or allow the user to enter the feed of interest manually in an EditView. To minimize the non-XML related code in this tutorial, the RSS feed URL is simply hard coded into the source. A menu is implemented as a hook to add RSS feed selections, as desired.
Before you can do any fancy parsing and data manipulation of an RSS feed, you must retrieve it from the Internet. This means that you connect to the site hosting the RSS feed through an Internet connection (cellular or WiFi) and perform an HTTP GET operation to retrieve the RSS data. The data which comes back is not a file, it is a stream of XML data. The URL class is employed to fetch the data.
You can parse XML data with multiple mechanisms. All of them involve the navigation of
the data stream and delineation of one data element from another with the opportunity to
store the data. Different kinds of XML Parsers are available in the Android SDK,
in addition to the option to create your own. The Android SDK includes provisions for the two most popular approaches, namely the DOM Parser, as well as the SAX Parser. The DOM approach is well suited for complex XML documents as it builds a node-oriented representation of the XML data in memory. The SAX approach uses callbacks whenever new tags are encountered, allowing the application to store only the data it is interested in. Due to the simplistic nature of the RSS XML structure, the SAX Parser is employed in this tutorial. The tutorial has a class named
RSSHandler which implements the SAX Parser callback functions.
The RSS feed extracted from the XML data stream must be put into a useful form. The tutorial has two helper classes:
RSSItem, which store the parsed form of the RSS XML data stream in memory. Once the XML data stream is fully parsed, the application interacts with these classes to render the information.
The tutorial's sample application employs two
classes to provide the user interface. The primary screen lists the RSS feed title and
publication date followed by a list of the RSS items. Once an item is selected through
a tap, or enter in the Android Emulator, the
Activity displays the full detail including the Title, Publication Date, Description and
Link elements of the RSS item. The user interface is set up so any links such as e-mail
or Web contained in the text are active—if you select them, the appropriate
action takes place. For example, if you select the link text, the Android browser
launches with the link as the target. In this way, the RSS reader allows all three levels of information to be accessed very intuitively. This is the power of RSS in general, and more specifically the efficacy of RSS on a mobile platform.
Refreshing data on a periodic basis is an important aspect to an RSS reader, as is the ability to access the information when off-line, such as when you fly on an airplane and your Android device is in flight mode. Some provision has been made for this in the sample application in the form of a menu hook for refreshing the data, but data persistence and scheduling are beyond the scope of this tutorial.
Next you'll build an application to obtain and display an RSS feed in an Android application.