Version 0.7
Copyright © 2009 - 2011 Lars Vogel
07.03.2011
Revision History | ||
---|---|---|
Revision 0.1 | 19.07.2010 | Lars Vogel |
Created | ||
Revision 0.2 - 0.7 | 30.08.2010 - 07.03.2011 | Lars Vogel |
bug fixes and enhancements |
Table of Contents
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 (java.net package). Even if you use the java.net 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.
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.
The following assumes that you have already basic knowledge in Android development .
Create the project "de.vogella.android.network.html" with the activity "ReadWebpage". Change the layout "main.xml" to the following.
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.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="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" android:scrollbars="vertical"></TextView> </LinearLayout>
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.
package de.vogella.android.network.html; import java.io.BufferedReader; import java.io.InputStreamReader; 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.app.Activity; 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. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); urlText = (EditText) findViewById(R.id.address); textView = (TextView) findViewById(R.id.pagetext); loadPreferences(); urlText.setText(lastUrl); } /** * Demonstrates loading of preferences The last value in the URL string will * be loaded */ private void loadPreferences() { SharedPreferences preferences = getSharedPreferences(PREFERENCES, Activity.MODE_PRIVATE); // Set this to the Google Homepage lastUrl = preferences.getString(URL, "http://209.85.229.147"); } @Override protected void onPause() { super.onPause(); SharedPreferences preferences = getSharedPreferences(PREFERENCES, Activity.MODE_PRIVATE); Editor preferenceEditor = preferences.edit(); preferenceEditor.putString(URL, urlText.getText().toString()); // You have to commit otherwise the changes will not be remembered preferenceEditor.commit(); } public void myClickHandler(View view) { switch (view.getId()) { case R.id.ReadWebPage: try { textView.setText(""); 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( response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { textView.append(line); } } catch (Exception e) { System.out.println("Nay, did not work"); textView.setText(e.getMessage()); } break; } } }
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.
To change the proxy settings you have to have the permission "android.permission.WRITE_SETTINGS" in "AndroidManifest.xml".
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