network
Using the HTTP API
In this example we shall show you how to use an HTTP Service from an HTTP Service provider with Android. This functionality is particularly useful when you want to display information to your Application, coming from another Web Site or generally from another service. The provider will give you a key that grants access to the services. You can then send HTTP requests and receive HTTP responses to get the content you want.
For using the HTTP framework the following steps must be followed:
- We will use the
DefaultHttpClient
classs for the execution of the HTTP Requests - The HTTP Client will provide an
HttpResponse
object that will contain the HTTP Response from the service. - For a successful HTTP Response use the
HttpEntity
object that contains the actual response data. - Use
toString
,getContent
of theHttpEntity
class to create anInputStream
to read the data
These are demonstrated in the code snippet(s) below:
package com.javacodegeeks.android.apps.moviesearchapp.services; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import com.javacodegeeks.android.apps.moviesearchapp.io.FlushedInputStream; import com.javacodegeeks.android.apps.moviesearchapp.util.Utils; public class HttpRetriever { private DefaultHttpClient client = new DefaultHttpClient(); public String retrieve(String url) { HttpGet getRequest = new HttpGet(url); try { HttpResponse getResponse = client.execute(getRequest); final int statusCode = getResponse.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url); return null; } HttpEntity getResponseEntity = getResponse.getEntity(); if (getResponseEntity != null) { return EntityUtils.toString(getResponseEntity); } } catch (IOException e) { getRequest.abort(); Log.w(getClass().getSimpleName(), "Error for URL " + url, e); } return null; } public InputStream retrieveStream(String url) { HttpGet getRequest = new HttpGet(url); try { HttpResponse getResponse = client.execute(getRequest); final int statusCode = getResponse.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url); return null; } HttpEntity getResponseEntity = getResponse.getEntity(); return getResponseEntity.getContent(); } catch (IOException e) { getRequest.abort(); Log.w(getClass().getSimpleName(), "Error for URL " + url, e); } return null; } public Bitmap retrieveBitmap(String url) throws Exception { InputStream inputStream = null; try { inputStream = this.retrieveStream(url); final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream)); return bitmap; } finally { Utils.closeStreamQuietly(inputStream); } } }
package com.javacodegeeks.android.apps.moviesearchapp.io; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; public class FlushedInputStream extends FilterInputStream { public FlushedInputStream(InputStream inputStream) { super(inputStream); } @Override public long skip(long n) throws IOException { long totalBytesSkipped = 0L; while (totalBytesSkipped < n) { long bytesSkipped = in.skip(n - totalBytesSkipped); if (bytesSkipped == 0L) { int b = read(); if (b < 0) { break; // we reached EOF } else { bytesSkipped = 1; // we read one byte } } totalBytesSkipped += bytesSkipped; } return totalBytesSkipped; } }
package com.javacodegeeks.android.apps.moviesearchapp.util; import java.io.IOException; import java.io.InputStream; public class Utils { public static void closeStreamQuietly(InputStream inputStream) { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { // ignore exception } } }
This was an example on how to use the HTTP API in Android.
Related Article: