Home » Android » core » network » Using the HTTP API

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

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:

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:

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz