Version 2.3
Copyright © 2010, 2011, 2012 Lars Vogel
20.04.2012
| Revision History | |||
|---|---|---|---|
| Revision 0.1 | 19.07.2010 | Lars Vogel |
Created |
| Revision 0.2 - 2.3 | 30.08.2010 - 20.04.2012 | Lars Vogel |
bug fixes and enhancements |
Table of Contents
Android contains the standard Java network
java.net
package which can be used to access network resources. Android
also
contains the
Apache HttpClient library.
The base class for HTTP network access in the
java.net
package is the
HttpURLConnection
class.
The preferred way of accessing the Internet according to Google is
the
HttpURLConnection
class, as Google is focusing their efforts on improving this
implementation.
Within an Android application you should avoid performing long running operations on the user interface thread. This includes file and network access.
StrictMode
allows to setup policies in your application to avoid doing
incorrect
things. As of Android 3.0 (Honeycomb)
StrictMode
is configured to crash with a
NetworkOnMainThreadException
exception, if network is accessed in the user interface thread.
While you should do network access in a background thread, this tutorial will avoid this to allow the user to learn network access independent from background processing.
If you are targeting Android 3.0 or higher, you can turn this check
off via
the following code at the beginning of your
onCreate()
method of your
activity.
StrictMode.ThreadPolicy policy = new StrictMode. ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
It is not advised to turn this of but in this tutorial we use this setting to be able to explain network access independently from background processing.
HttpURLConnection
which is also available in standard Java,
is a general-purpose,
lightweight HTTP client suitable for most
applications.
In the latest version
HttpURLConnection
supports the transparent response compression (via the header
Accept-Encoding: gzip, Server Name Indication (extension of SSL and TLS) and a response
cache.
The API is relatively straight forward. For example to retrieve the webpage www.vogella.com.
// Somewhere in your code this is called // in a thread which is not the user interface // thread try { URL url = new URL("http://www.vogella.com"); HttpURLConnection con = (HttpURLConnection) url .openConnection(); readStream(con.getInputStream()); } catch (Exception e) { e.printStackTrace(); } private void readStream(InputStream in) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
The Javadoc of
HttpURLConnection
suggest to not reuse on
HttpURLConnection. If you use it this way,
HttpURLConnection
has no threading issues, as it will not be shared between different
Threads.
Obviously the network on an Android device is not always available. You can check the network is currently available via the following code.
public boolean isNetworkAvailable() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); // if no network is available networkInfo will be null // otherwise check if we are connected if (networkInfo != null && networkInfo.isConnected()) { return true; } return false; }
This requires the
ACCESS_NETWORK_STATE
permission.
This chapter is only relevant for you if you are testing with the
Android simulator behind a proxy.
You can set
the proxy via the
Settings
class. 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
android.permission.WRITE_SETTINGS
permission
in your
AndroidManifest.xml file.

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
Introduction to Android Development
Android Location API and Google Maps
Crest Framework to access rest services, works also on Android
vogella Training Android and Eclipse Training from the vogella team
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system