Introduction

This article explains how to post data using HttpClient in Android. Android Studio is used to create the sample.
HttpClient is used when you want to receive and send data from the server over the internet. So for this you need to create a http client using HttpClient class. First, you will create the object of Http client and the URL to the constructor of HttpPost class that post the data. Now create the data that you want to send to the server. Set the entity with the data and get the response by ing httppost to the constructor of HttpResponse that returns the response. Get the statusLine that the send data is valid or not. If the status code returns by the getStatus() are 200 it means your data is sent.
Step 1
Create a project like this:
image which show data
Step 2
Create a an XML file and write this
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello_world" />
  14. </RelativeLayout>
Step 3
Create a Java class file with the following:
  1. package com.androidhttpclient;
  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.StatusLine;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.impl.client.DefaultHttpClient;
  14. import org.apache.http.message.BasicNameValuePair;
  15. import android.annotation.TargetApi;
  16. import android.app.Activity;
  17. import android.os.Build;
  18. import android.os.Bundle;
  19. import android.os.StrictMode;
  20. import android.util.Log;
  21. public class MainActivity extends Activity {
  22. @TargetApi(Build.VERSION_CODES.GINGERBREAD)
  23. @Override
  24. public void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27. if (android.os.Build.VERSION.SDK_INT > 9) {
  28. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  29. StrictMode.setThreadPolicy(policy);
  30. }
  31. // Creating HTTP client
  32. HttpClient Client = new DefaultHttpClient();
  33. // Creating HTTP Post
  34. HttpPost Post = new HttpPost(
  35. "http://androidexample.com/media/webservice/httppost.php");
  36. Log.d("HTTPPSt",""+Post);
  37. // Building post parameters
  38. // key and value pair
  39. List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
  40. nameValuePair.add(new BasicNameValuePair("name", "username"));
  41. nameValuePair.add(new BasicNameValuePair("word","8090509239"));
  42. // Url Encoding the POST parameters
  43. try {
  44. Post.setEntity(new UrlEncodedFormEntity(nameValuePair));
  45. } catch (UnsupportedEncodingException e) {
  46. // writing error to Log
  47. e.printStackTrace();
  48. }
  49. // Making HTTP Request
  50. try {
  51. HttpResponse response = Client.execute(Post);
  52. StatusLine statusLine=response.getStatusLine();
  53. // writing response to log
  54. Log.d("Http Response:", response.toString());
  55. Log.d("statusline:", ""+statusLine);
  56. } catch (ClientProtocolException e) {
  57. // writing exception to log
  58. e.printStackTrace();
  59. } catch (IOException e) {
  60. // writing exception to log
  61. e.printStackTrace();
  62. }
  63. }
  64. }
Step 4
Use the following for the Android manifest.xml file:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.androidhttpclient"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.androidhttpclient.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  24. </manifest>
Step 5
The read response and statusline returned from the server-side:
JsonArrayData