Sending Post Data in Android

Sending POST data in Android

Note (Oct 2020): AsyncTask used in the following answer has been deprecated in Android API level 30. Please refer to Official documentation or this blog post for a more updated example

Updated (June 2017) Answer which works on Android 6.0+. Thanks to @Rohit Suthar, @Tamis Bolvari and @sudhiskr for the comments.

    public class CallAPI extends AsyncTask<String, String, String> {

public CallAPI(){
//set context variables if required

protected void onPreExecute() {

protected String doInBackground(String... params) {
String urlString = params[0]; // URL to call
String data = params[1]; //data to post
OutputStream out = null;

try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
out = new BufferedOutputStream(urlConnection.getOutputStream());

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));

} catch (Exception e) {


  • How to add parameters to HttpURLConnection using POST using NameValuePair

Original Answer (May 2010)

Note: This solution is outdated. It only works on Android devices up to 5.1. Android 6.0 and above do not include the Apache http client used in this answer.

Http Client from Apache Commons is the way to go. It is already included in android. Here's a simple example of how to do HTTP Post using it.

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("");

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block

Sending POST request in android studio

to answer your question #3 would suggest using a library like OkHTTP to make that post request. That will make your code way simpler and easier to debug.

Make sure you have the following permissions on your Manifest:

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

Add the library to your gradle file:

compile 'com.squareup.okhttp3:okhttp:3.10.0'

Then, change your onCreate method to the following:

private final OkHttpClient client = new OkHttpClient();

protected void onCreate(Bundle savedInstanceState) {

ActionBar actionBar = this.getSupportActionBar();


private void makePost(){
RequestBody requestBody = new MultipartBody.Builder()
.addFormDataPart("email", "")
.addFormDataPart("name", "your-name")

request = new Request.Builder()

try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println( + ": " + responseHeaders.value(i));


And this should make a post request to your endpoint.

If you wanna log it, you can just add a logging interceptor to it.

Hope this helps you out!

How to send http post request from Android?

I am using this code and working as well. Try this code.

public static String httpPostRequest(Context context, String url, String email) {
String response = "";
BufferedReader reader = null;
HttpURLConnection conn = null;
try {
LogUtils.d("RequestManager", url + " ");
LogUtils.e("data::", " " + data);
URL urlObj = new URL(url);

conn = (HttpURLConnection) urlObj.openConnection();
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

data += "&" + URLEncoder.encode("Email", "UTF-8") + "="
+ URLEncoder.encode(email, "UTF-8");


LogUtils.d("post response code", conn.getResponseCode() + " ");

int responseCode = conn.getResponseCode();

reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;

while ((line = reader.readLine()) != null) {
sb.append(line + "\n");

response = sb.toString();
} catch (Exception e) {
LogUtils.d("Error", "error");
} finally {
try {
if (conn != null) {
} catch (Exception ex) {
LogUtils.d("RESPONSE POST", response);
return response;

Send Post Request with params using Retrofit

I have found the solution. The issue was a problem in my classes structure. So i updated them like the following samples.

public class LandingPageReport {

private ArrayList<LandingPageReportItem> GetDetailWithMonthWithCodeResult;

// + Getter Setter methods

public class LandingPageReportItem {

private String code;

private String field1;

// + Getter Setter methods

And then i use this retrofit configuration

void getLandingPageReport(@Field("code") String code,
@Field("monthact") String monthact,
Callback<LandingPageReport> cb);

Related Topics

Leave a reply