Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Build a mobile RSS reader

Handle XML with Android

Frank Ableson is an entrepreneur and software developer in northern New Jersey, specializing in mobile and embedded application software. He is currently authoring a book about Android application development for Manning Publications. His professional interests are embedded systems, wireless communications, and automotive electronics. His biggest fans are his wife, Nikki, and their children.

Summary:  What good is a mobile computing environment if you can't keep track of your favorite news feeds on the go? Sure, you can use Android's browser to read your favorite Web sites, but it's unlikely the sites are optimized for a screen two inches high. And besides, then you'll miss the opportunity to integrate RSS or other XML data with other mobile applications to make your own mash-ups. This tutorial shows you how to use the Android Developer Tools to read, parse, and display XML data.

Date:  18 Mar 2008
Level:  Intermediate PDF:  A4 and Letter (328 KB | 33 pages)Get Adobe® Reader®

Activity:  178059 views
Comments:  

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 major requirements of an RSS reader application

The following sections detail how the Android RSS reader addresses the major requirements of an RSS reader application.

Specify the RSS feed of interest

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.

Obtain the RSS feed of interest

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.

Parse the XML data stream

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.

Store the RSS data

The RSS feed extracted from the XML data stream must be put into a useful form. The tutorial has two helper classes: RSSFeed and 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.

Rendering the RSS feed

The tutorial's sample application employs two Activity 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 ShowDescription 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.

Data refreshing and off-line viewing

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.

3 of 9 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML, Open source
ArticleID=295231
TutorialTitle=Build a mobile RSS reader
publish-date=03182008
author1-email=fableson@msiservices.com
author1-email-cc=dwxed@us.ibm.com