Home > DeveloperSection > Blogs > How to send JSON data to server with http post request

How to send JSON data to server with http post request

Android JSON  Http Post  Http Request  Android Json 
0 Comment(s)
 595  View(s)
Rate this:

How to send JSON data to server with http post request 

JSON stands for JavaScript Object Notation. It is an independent data exchange format and is the best alternative for XML. This chapter explains how to parse the JSON file and extract necessary information from it.

JSON format


    "Student": [


                "id": "std101",

                "name": "Zack Desouja",

                "phone": ": "+91 0000000000"



                "id": "std102",

                "name": "Johnny Depp",

                "phone": "mobile": "+91 0000000000"





Here I am creating a sample for send JSON data to server and get response from server.

1.      Add below method in your activity.

      public String postDataToServer(String id,String name,String phone) {

          String sResponse = "";

          try {


// create JSONObj object and add the parameters


    JSONObject jsonObj = new JSONObject();



if (android.os.Build.VERSION.SDK_INT > 9) {

                  StrictMode.ThreadPolicy policy = new                           StrictMode.ThreadPolicy.Builder().permitAll().build();






           //Add parameter in json

              jsonObj.put("Id", id);

              jsonObj.put("SName", name);

              jsonObj.put("phone", phone);


          // create the POST object and add set url

              HttpPost httpPost = new HttpPost(

                        " your server url”

              StringEntity entity = new StringEntity(jsonObj.toString(),




              HttpClient client = new DefaultHttpClient();

              HttpResponse response = client.execute(httpPost);


   // get response code from server

   int responsecode =response.getStatusLine().getStatusCode();


    // get response String from server


    StringBuilder builder = new StringBuilder();

    BufferedReader bReader = new BufferedReader(                         new InputStreamReader(response.getEntity().getContent()));


              String line = "";

              while ((line = bReader.readLine()) != null) {



              sResponse = builder.toString();

          } catch (Exception ex) {


          return sResponse;


// Call the method for send data to server


   String data=postDataToServer("st101","Zack Desouja","+91 0000000000");


2. Add permission for internet in your manifest file


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


Don't want to miss updates? Please click the below button!

Follow MindStick