RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Connecting to the Web: I/O Programming in Android : Page 3

Learn how to connect your Android app to the web using an HTTP connection and how to parse XML files to extract useful information.

Downloading RSS Feeds
Very often, you need to download XML files and parse the contents (a good example of this is consuming web services). And so in this section, you will learn how to download a RSS feed and then extract the relevant parts (such as the <title> element) and display its content.

Figure 4. An Example: The "<item>/<title>" element contains the title of each posting.

Define the DownloadRSS() function as shown in Listing 5.

First, call the OpenHttpConnnection() function to obtain an InputStream object. To process XML documents, use the following classes:

  • Document: This represents an XML document.
  • DocumentBuilder: This converts a XML source (such as files, streams, and so on) into a Document.
  • DocumentBuilderFactory: This provides a factory for DocumentBuilder instances.
Essentially, the InputStream object is used to read the XML data and then parsed into a Document object.

After the XML document is loaded into a Document object, you locate the relevant elements to extract. In particular, for an RSS document, the "<item>/<title>" element contains the title of each posting (see Figure 4).

Figure 5. The RSS Feed: Displaying all the titles of the postings in a RSS feed using the Toast class.

Once the title of each posting is retrieved, it is displayed using the Toast class. To test the DownloadRSS() function, modify the onCreate() event as shown in Listing 6.

Press F11 in Eclipse to test the application on the Android emulator. Figure 5 shows the titles of all the postings contained within the RSS feed displayed by the Toast class.

That’s it! If you have interesting ideas involving things you can do with HTTP downloads, send me an email.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date