Android Programming – Making a HTTP Get Request From Application

We will be starting mid level to advanced level Android Tutorials for everyone wanting to learn the android and how to program for android. In this series we are starting with the article which will explain how to make HTTP get requests from your application.

In order to make HTTP Get requests from your applications you have to make use of the java/apache’s HTTPClient class. The http client class can be used to pass on a Get request as well as a Post request. In this article we will be examining how to make the Get request to the server using the HTTPClient class of apache.

First make sure you have the necessary class imported into your java project. On top of the class add the following lines to import the classes needed to make the get request to the server.

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

The above imports will add in the necessary classes to your project to start a HTTP request and fetch result from the same.

Next step is to make the request, to do the same please use the following code:

String url = “http://www.vastal.com/”;

HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);

HttpResponse response = client.execute(httpGet);

StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();

if(statusCode == 200)
{
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while((line = reader.readLine()) != null)
{

// Do what you want to do with the data fetched here…
}
}

In the above code,

HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);

is used to make an object of the HttpClient and HttpGet classes which will be used to make the Http request to the server. The HttpGet class accepts the URL you want to access as it’s argument which is passed to class via the constructor.

The next part:

HttpResponse response = client.execute(httpGet);

StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();

The HttpResponse class is used to get reaponse of the web server while making the Http request. It is always a good practice to get response and check for validity of the response before making use of the data being fetched from the server.

The statusCode variable in the above example contains the status of the page being requested by the server. In above example we have checked if the status is 200 or not. If it is 200 i.e. OK status from web server we will start reading the response from the web server into the HttpEntity class. The entity is then passed on to InputStream which is then used to get string data using Buffer.

We will keep updating our Android Programming Tips section every 4 days, please subscribe to our feed and also share our articles on Facebook and Google Plus so other’s can benefit from our insight. Please feel free to send us your comments below, on how we can improve.

No comments yet.

Leave a Reply