October 25, 2011 08:28 PM

Android making HTTP Requests



In most of the android applications it is essential that app may need to connect to internet and make some HTTP requests. In this article i’ll be demonstrating about making simple HTTP Requests in android.

Below are the code snippets to handle HTTP requests.

Download Code


Creating HTTP Client and HTTP Post

// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();

// Creating HTTP Post
HttpPost httpPost = new HttpPost("http://www.example.com/login");

 

Bulding Post Parameters

The following code will create post parameters pair with key and value.

// Building post parameters, key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
nameValuePair.add(new BasicNameValuePair("password", "encrypted_password"));

 

URL Encoding POST data

Before making HTTP request you need to encode the post data in order to convert all string data into valid url format.

// Url Encoding the POST parameters
try {
	httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
}
catch (UnsupportedEncodingException e) {
	// writing error to Log
	e.printStackTrace();
}

 

Finally making HTTP Request

Finally you need to execute httpPost using the httpClient created before.

// Making HTTP Request
try {
	HttpResponse response = httpClient.execute(httpPost);

	// writing response to log
	Log.d("Http Response:", response.toString());

} catch (ClientProtocolException e) {
	// writing exception to log
	e.printStackTrace();
		
} catch (IOException e) {
	// writing exception to log
	e.printStackTrace();
}

 

Final Code

The following is the final code to make http Requests. I am writing response to log. Check your Log report in Eclipse to see your http response.

package com.androidhive.httprequests;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class AndroidHTTPRequestsActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Creating HTTP client
		HttpClient httpClient = new DefaultHttpClient();
		// Creating HTTP Post
		HttpPost httpPost = new HttpPost(
				"http://www.example.com/login");

		// Building post parameters
		// key and value pair
		List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
		nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
		nameValuePair.add(new BasicNameValuePair("message",
				"Hi, trying Android HTTP post!"));

		// Url Encoding the POST parameters
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
		} catch (UnsupportedEncodingException e) {
			// writing error to Log
			e.printStackTrace();
		}

		// Making HTTP Request
		try {
			HttpResponse response = httpClient.execute(httpPost);

			// writing response to log
			Log.d("Http Response:", response.toString());
		} catch (ClientProtocolException e) {
			// writing exception to log
			e.printStackTrace();
		} catch (IOException e) {
			// writing exception to log
			e.printStackTrace();

		}
	}
}

(If you find any error either in code or content please help me in improvising the content)

THANK YOU
Oops! We are unable to process your request at this moment. Please try again.