Tuesday, January 26, 2010

Android : working with Http

All the developers working in Mobile application development, using Java technology, are very much familiar with J2ME & BlackBerry API. Now a days, it is almost impossible to imagine a mobile application without data connection . Reason being that mobile is being used as a smart client for many kind of services, managed by different kind of servers.

While J2ME & BB share same API for data connections (At least at core level and some additional work is required for BB), Android exposes entirely different API for data connections. Following is the example for creating data connection using Android API for http connections.

private String getResponse(String url,String parameters, final String method) throws IOException {
HttpURLConnection connection = null;
OutputStreamWriter out = null;
DataInputStream stream=null;
try {
if (url != null && parameters != null) {
/** Opening the connection **/
URL myURL = new URL( url);
/* Open a connection to that URL. */
connection =(HttpURLConnection) myURL.openConnection();
// connection = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
/** Set the request method **/

/** Add headers if necessary **/
// Setup data if you want to post some data
if (parameters != null) {

connection.setRequestProperty("Content-Length", String.valueOf(parameters.length()));
out = new OutputStreamWriter(connection.getOutputStream());
stream = new DataInputStream(connection.getInputStream());
/** Get the response code **/
httpCode = connection.getResponseCode();

/** Get the response message **/
switch (httpCode) {
case HttpURLConnection.HTTP_OK:
/** Get the input stream **/

byte[] data = new byte[512];
int len = 0;
ByteArrayOutputStream buffer=new ByteArrayOutputStream();
while (-1 != (len = stream.read(data))) {
buffer.write(data, 0, len);
return new String(buffer.toByteArray());

throw new IOException("Error: HTTP " + httpCode);

} catch (IOException e) {

throw new IOException("Error: HTTP " + httpCode + " " + e.getMessage());

} finally {
if (out != null) {
try {
} catch (IOException e) {

} finally {
out = null;
if (stream != null) {
try {
} catch (IOException e) {}
finally {
stream = null;
if (connection != null) {
try {

} catch (Exception e) {}
finally {
connection = null;
return null;

Code above shows that how to use the API available from Android to open a data connection and fetching the data.Though return of data may vary based on the specific need and implementation can be changed accordingly.

Main difference between J2ME and Android, in terms of API is , J2ME uses generic connection framework for any kind of data connection while Android uses standard classes(URL & HttpURLConnection) from java.net (J2SE) package.

URL represents a uniform resource locator , which is used to open HttpURLConnection.
HttpURLConnection connection =(HttpURLConnection) myURL.openConnection();
Following two lines makes connection available for writing and reading data from connection stream.
connection.setDoOutput(true); // Makes connection available for reading
connection.setDoInput(true); // Makes connection available for writing.

Hope this will help to understand http data connection on Android platform.

No comments:

Post a Comment