Tuesday 14 February 2012

Java tip: Set up an RSS feed for your Android application

Use Java's SAXParser to retrieve and parse an RSS feed for Android. This Java tip is for developers new to Android and includes instructions for setting up an Android App development environment and a short application tutorial.

An RSS feed is an XML-formatted file used to publish periodically updated syndicated information. An RSS feed may be parsed (that is, read and formatted) using an XML parser. Java-compatible parsers used to parse XML on Android include:
  • android.content.res.XmlResourceParser is a pull parser
  • Simple API for XML (SAX) is found in the org.xml.sax package
  • Android ROME Feed Reader is Google’s RSS feed reader for Android
  • Android Feed Reader is another Google RSS/Atom feed reader for Android
  • Android-rss is a lightweight Android library for RSS 2.0 feeds
This Java tip is a step-by-step introduction to using the javax.xml.parsers.SAXParser to parse an RSS feed in XML format. SAXParser is a standard choice because it is included with the Android APIs in the Android SDK. We'll set up the development environment, create a simple Android app and feed for demonstration purposes, and then use SAXParser to format the feed for Android. While some familiarity with Java application development is assumed, this tip is suitable for developers new to Java mobile development.

Setting up the environment

Take the following steps to set up the development environment for this project:
  1. Install the Eclipse IDE.
  2. Install the Android Development Tools (ADT) plugin for Eclipse. The ADT plugin for Eclipse provides a set of extensions to develop Android applications in Eclipse.
  3. Install Android 2.3 SDK platform. Android SDK provides tools for developing Android applications.
  4. Create an Android Virtual Device and set the Target environment as Android 2.3.3. API Level 10.

The Android project

We'll create an example Android project to receive the RSS feed.
  1. In your Eclipse IDE select File-->New.
  2. In New select Android-->Android Project, then click Next.
  3. In the New Android Project window, specify a Project name (RSSFeed).
  4. Select Android Platform 2.3 API 10 for your Build Target.
  5. In Properties, specify an Application name (again, RSSFeed), and a Package name (android.rss).
  6. Select the checkbox: Create Activity, and specify the Activity class (RssFeed).
  7. Specify the minimum SDK version as 10 and click Next; or, if you've selected the Platform 3.0 API 11, then specify the minimum SDK Version as 11.
Note that an activity (Step 6) represents a user interaction. The class extending the Activity class creates a window for a UI.
The resulting Android project will consist of the following files:
  1. An activity class (RSSFeed), which extends Activity.
  2. A res/layout/main.xml file, which specifies the layout of the Android UI components.
  3. An AndroidManifest.xml file, which contains application configuration such as the package name, the main activity to launch when the Android application is started, application components, processes, permissions, and the minimum API level.
In res/layout/main.xml, specify the layout of the Android UI components. Create a LinearLayout and set android:orientation as "vertical." Our goal is to display the RSS feed as a text message, so add a TextView element for the title of the RSS feed and specify android:text as a Google RSS Feed. Add a TextView element, with id "rss" to display the RSS feed. Listing 1 shows the resulting main.xml.

Original Source

0 comments:

Post a Comment