Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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


advertisement
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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap