Wednesday, June 29, 2011

Android Networking - Tutorial

Lars Vogel

Version 0.7
Revision History
Revision Vogel
Revision 0.2 - 0.730.08.2010 - 07.03.2011Lars Vogel
bug fixes and enhancements
Networking with Android
This article describes how to access web resources in Android. It is based on Eclipse 3.6, Java 1.6 and Android 2.3 (Gingerbread).

1. Android Networking

1.1. Networking

Android contains the Apache HttpClient library and this library is the preferred way of performing network operations in Android. Android also allows to access the network via the standard Java Networking API ( package). Even if you use the package Android will internally use the Apache library.
For XML parsing Android provides the class "XmlPullParser" which is an Android specific XML parser. The standard SAX and DOM XML parsers are also available on Android. The Javadoc of "XmlPullParser" gives a nice example how you can use this library.
As of Android 2.2 you can also use the AndroidHttpClient. An instance can be received via newInstance() which allows to specify the user agent. AndroidHttpClient supports SSL and has utility methods for GZIP compressed data.
To access the internet your application requires the "android.permission.INTERNET" permission.

1.2. Android Basics

The following assumes that you have already basic knowledge in Android development .

2. URL example

Create the project "" with the activity "ReadWebpage". Change the layout "main.xml" to the following.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
 android:orientation="vertical" android:layout_width="fill_parent"

 <LinearLayout android:id="@+id/LinearLayout01"
  android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
 <EditText android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/address"></EditText>
 <Button android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:text="Read Webpage"
  android:id="@+id/ReadWebPage" android:onClick="myClickHandler"></Button>
 <TextView android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:id="@+id/pagetext"


Add the permission "android.permission.INTERNET" to "AndroidManifest.xml" to allow your application to access the internet.
Create the following code to read a webpage and show the HTML code in the TextView.
This example also demonstrate the usage of Android preferences to store user data. The URL which the user has typed is stored in the preferences in the method onPause(). This method is called whenever the Activity is send into the background.



import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class ReadWebpage extends Activity {
 private static final String PREFERENCES = "PREFERENCES";
 private static final String URL = "url";
 private String lastUrl;
 private EditText urlText;
 private TextView textView;

 /** Called when the activity is first created. */

 public void onCreate(Bundle savedInstanceState) {

  urlText = (EditText) findViewById(;
  textView = (TextView) findViewById(;


  * Demonstrates loading of preferences The last value in the URL string will
  * be loaded
 private void loadPreferences() {
  SharedPreferences preferences = getSharedPreferences(PREFERENCES,
  // Set this to the Google Homepage
  lastUrl = preferences.getString(URL, "");

 protected void onPause() {
  SharedPreferences preferences = getSharedPreferences(PREFERENCES,
  Editor preferenceEditor = preferences.edit();
  preferenceEditor.putString(URL, urlText.getText().toString());
  // You have to commit otherwise the changes will not be remembered

 public void myClickHandler(View view) {
  switch (view.getId()) {
   try {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(urlText.getText().toString());
    HttpResponse response = client.execute(request);
    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(
    String line = "";
    while ((line = rd.readLine()) != null) {

   catch (Exception e) {
    System.out.println("Nay, did not work");


3. Proxy

This chapter is only relevant for you if you are testing with the Android similator behind a proxy. In class you are behind a proxy during your testing you can set the proxy via the class "Settings". For example you could add the following line to your onCreate method in your activity.

Settings.System.putString(getContentResolver(), Settings.System.HTTP_PROXY, "myproxy:8080");

To change the proxy settings you have to have the permission "android.permission.WRITE_SETTINGS" in "AndroidManifest.xml".

No comments:

Post a Comment