o far all the previous articles in this Android series have been concerned with location-based services and the use of Google Maps for mapping applications. In this article, let’s turn our attention to some bread-and-butter issues?like connecting your Android application to the Web to download files.
Very often, you need to connect your Android application to the outside world, such as downloading images as well as consuming web services. This article will show you how to make your Android application communicate with the outside world using an HTTP connection. You’ll also learn how to parse XML files so that useful information can be extracted from XML documents.
![]() |
|
Figure 1. The New Project: The new Android project is called HttpDownload. |
Creating the Project
Using Eclipse, create a new Android project and name it HttpDownload, as shown in Figure 1.
In the HttpDownload.java file, first import the various namespaces that you will need for this project (see Listing 1).
As you’ll be accessing the Internet, you’ll need to add the relevant permissions to your AndroidManifest.xml file:
Let’s define a helper function called OpenHttpConnection() that opens a connection to a HTTP server and returns an InputStream object (Listing 2).
To open a connection to a server, you first create an instance of the URL class and initialize it with the URL of the server. When the connection is established, you pass this connection to an URLConnection object. You then verify whether the protocol is indeed HTTP; if not you will throw an IOException. The URLConnection object is then cast into an HttpURLConnection object and you set the various properties of the HTTP connection. Next, you connect to the HTTP server and get a response from the server. If the response code is HTTP_OK, you then get the InputStream object from the connection so that you can begin to read incoming data from the server. The function then returns the InputStream object obtained.
In the main.xml file, insert the
Downloading Images
The first thing you want to do is to download some images stored on a web server. To do this, define the DownloadImage() function as follows:
private Bitmap DownloadImage(String URL) { Bitmap bitmap = null; InputStream in = null; try { in = OpenHttpConnection(URL); bitmap = BitmapFactory.decodeStream(in); in.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return bitmap; }
The DownloadImage() function takes in a string containing the URL of the image to download. It then calls the OpenHttpConnection() function to obtain an InputStream object for reading the image data. The InputStream object is sent to the decodeStream() method of the BitmapFactory class. The decodeStream() method decodes an InputStream object into a bitmap. The decoded bitmap is then returned by the DownloadImage() function.
To test the DownloadImage() function, modify the onCreate() event as follows:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Bitmap bitmap = DownloadImage( "http://www.streetcar.org/mim/cable/images/cable-01.jpg"); img = (ImageView) findViewById(R.id.img); img.setImageBitmap(bitmap); }
Press F11 in Eclipse to test the application on the Android emulator. Figure 2 shows the image downloaded and displayed in the ImageView view.
![]() Figure 2. The Downloaded Image: The image is downloaded and displayed in the ImageView view. |
![]() Figure 3. The Downloaded File: The text file is downloaded and displayed in a TextView view. |
Downloading Text
Now, let’s try to download text files from the web and display them using the TextView view. First, define the DownloadText() function as shown in Listing 4.
As usual, you call the OpenHttpConnnection() function to obtain an InputStream object. The InputStream object is then used by the InputStreamReader class so that characters can be read from the stream. The characters are read into a char array and then copied into a string variable. The string variable is then returned.
To test the DownloadText() function, modify the onCreate() event as follows:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* Bitmap bitmap = DownloadImage( "http://www.streetcar.org/mim/cable/images/cable-01.jpg"); img = (ImageView) findViewById(R.id.img); img.setImageBitmap(bitmap); */ String str = DownloadText("http://www.appleinsider.com/appleinsider.rss"); txt = (TextView) findViewById(R.id.text); txt.setText(str); }
Press F11 in Eclipse to test the application on the Android emulator. Figure 3 shows the file downloaded and displayed in the TextView view.
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
![]() |
|
Figure 4. An Example: The “ |
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 “
![]() |
|
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.