Android HTTP POST & GET Example

In Android HTTP POST & GET tutorial, I have explained how to send HTTP POST and GET requests programmatically in Android.

Below are topics covered in this article
1) Send HTTP POST request
2) Send HTTP GET request

We can use org.apache.http.client.HttpClient class to make HTTP requests.


 

Send HTTP POST request

Follow the steps to send HTTP POST requests.
1. Create an object of HttpClient

HttpClient httpClient = new DefaultHttpClient();

2. Create an object of HttpPost

HttpPost httpPost = new HttpPost("http://www.example.com");

3. Add POST parameters

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("username", "test_user"));
nameValuePair.add(new BasicNameValuePair("password", "123456789"));

4. Encode POST data

We need to encode our data into valid URL format before making HTTP request.

//Encoding POST data
try {
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));

} catch (UnsupportedEncodingException e) 
{
     e.printStackTrace();
}

 

5. Finally making an HTTP POST request

try {
	HttpResponse response = httpClient.execute(httpPost);
	// write response to log
	Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
	// Log exception
	e.printStackTrace();
} catch (IOException e) {
	// Log exception
	e.printStackTrace();
}

Make sure you add Internet permission to your app before making such requests else you might see some errors.

You can add Internet permissions by adding this line to your manifest file above application tag.

<uses-permission android:name="android.permission.INTERNET" />

Android HTTP POST Example

package com.example.httprequestexample;

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.HttpGet;
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 HTTPPostActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		makePostRequest();

	}
	private void makePostRequest() {

		
		HttpClient httpClient = new DefaultHttpClient();
								// replace with your url
		HttpPost httpPost = new HttpPost("www.example.com"); 


		//Post Data
		List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
		nameValuePair.add(new BasicNameValuePair("username", "test_user"));
		nameValuePair.add(new BasicNameValuePair("password", "123456789"));

		
		//Encoding POST data
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
		} catch (UnsupportedEncodingException e) {
			// log exception
			e.printStackTrace();
		}

		//making POST request.
		try {
			HttpResponse response = httpClient.execute(httpPost);
			// write response to log
			Log.d("Http Post Response:", response.toString());
		} catch (ClientProtocolException e) {
			// Log exception
			e.printStackTrace();
		} catch (IOException e) {
			// Log exception
			e.printStackTrace();
		}

	}

}

 

Send  HTTP GET Request

To send HTTP GET request follow the steps.
1. Create an object of HttpClient

HttpClient client = new DefaultHttpClient();

2. Create an object of HttpGet

HttpGet request = new HttpGet("http://www.example.com");

3. Finally make HTTP request

HttpResponse response;
try {
	response = client.execute(request);
			
	Log.d("Response of GET request", response.toString());
} catch (ClientProtocolException e) {
	// TODO Auto-generated catch block
        e.printStackTrace();
} catch (IOException e) {
      // TODO Auto-generated catch block
     e.printStackTrace();
}

Android HTTP GET Example

package com.example.httprequestexample;

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.HttpGet;
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 HTTPGETActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		makeGetRequest();

	}
	private void makeGetRequest() {

		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet("http://www.example.com"); 
								// replace with your url

		HttpResponse response;
		try {
			response = client.execute(request);
			
			Log.d("Response of GET request", response.toString());
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

Reference: Android Documenation


I am a Developer. My motto: "Language is not a barrier" http://hayageek.com
All posts by Ravishanker Kusuma