http://hmkcode.com/android-send-json-data-to-server/



Android | Send “POST” JSON Data to Server

android-post-jsonHTTP POST allows you to send data to the server in different formats such as XML, JSON or binary. Of course, the server should be ready to handle data in the selected format. This article shows how to send JSON data from Android app to a server that can handle it.

Objectives:

  • How to send JSON data to the server using HTTP POST?

Environment & Tools:

  • Android Developer Tools (ADT) (or Eclipse + ADT plugin)
  • AVD Nexus S Android 4.3 “emulator”
  • Min SDK 8
  • Server side service that can receive HTTP POST request with data in JSON format “http://hmkcode.appspot.com/jsonservlet

About this Sample:

  • Android App “we are building here” will format entered data “name, country and twitter account” into JSON format and send it to the server.
  • The server side is a Java Servlet “wep app deployed on Google GAE” that accepts POST request with data in JSON format. You can read about how to develop such servlet in Java Servlet Send & Receive JSON Using jQuery.ajax()
  • The server-side service provides UI to check that your data has been sent successfully.

( 1 ) Create Android Application

  • File >> New >> Android Application
  • App Name: POST JSON
  • Enter Project Name: android-post-json
  • Pakcage: com.hmkcode.android
  • Keep other defualt selections, go Next  till you reach Finish

( 2 ) The App Layout & Views

  • Main layout is LinearLayout
  • One TextView that shows connection status “the one in green”.
  • One child layout RelativeLayout holding data entry part.
  • Three TextViews “Name, Country & Twitter” baseline-aligned with their corresponding EditText
  • One Button to send “post” data to the server.
  • res/layout/activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/tvIsConnected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"    
        android:background="#FF0000"
        android:textColor="#FFF"
        android:textSize="18dp"
        android:layout_marginBottom="5dp"
        android:text="is connected?" />
 
   <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
 
       <TextView
            android:id="@+id/tvName"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="Name"
            android:layout_alignBaseline="@+id/etName"/>
       <EditText
            android:id="@+id/etName"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvName"/>
       <TextView
            android:id="@+id/tvCountry"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvName"
            android:text="Country"
            android:layout_alignBaseline="@+id/etCountry"/>
       <EditText
            android:id="@+id/etCountry"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvCountry"
            android:layout_below="@+id/etName"/>
       <TextView
            android:id="@+id/tvTwitter"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvCountry"
            android:text="Twitter"
            android:layout_alignBaseline="@+id/etTwitter"/>
       <EditText
            android:id="@+id/etTwitter"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tvTwitter"
            android:layout_below="@+id/etCountry"/>
   </RelativeLayout>
 
   <Button
       android:id="@+id/btnPost"
       android:layout_width="200dp"
       android:layout_height="wrap_content"
       android:layout_gravity="center_horizontal"
       android:text="POST"/>
 
</LinearLayout>

( 3 ) The Person Class

This is a simple model class to hold person information “name, country & twitter account”.

  • /src/com/hmkcode/android/vo/Person.java
1
2
3
4
5
6
7
8
9
10
11
package com.hmkcode.android.vo;
 
public class Person {
 
    private String name;
    private String country;
    private String twitter;
 
//getters & setters....
 
}

( 4 ) The Activity Class

  • onCreate()
    1. Get reference to the defined views in layout file
    2. Check for connection status
    3. Add click listener to the Post button
  • POST(url, Person)
      1. Open Http connection.
      2. Create HttpPOST object passing the url.
      3. Create Person object & convert it to JSON string.
      4. Add JSON to HttpPOST, set headers & send the POST request.
      5. Get the response Inputstream, convert it to String and return it.
  • HttpAsyncTask
    1. Helps in creating the connection in a separate thread so the UI will not freeze.
  • onClick
    1. This method is executed when user click on Post button
    2. It create HttpAsyncTask object and execute it.
  • isConnected()
    1. Check if there is network connection or not
  • validate()
    1. Helper method to check that data is not empty before send the request.
  • convertInputStreamToString()
    1. Helper method to convert inputstream to String
  • src/com/hmkcode/android/ActivityMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.hmkcode.android;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import com.hmkcode.android.vo.Person;
 
public class MainActivity extends Activity implements OnClickListener {
 
    TextView tvIsConnected;
    EditText etName,etCountry,etTwitter;
    Button btnPost;
 
    Person person;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // get reference to the views
        tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
        etName = (EditText) findViewById(R.id.etName);
        etCountry = (EditText) findViewById(R.id.etCountry);
        etTwitter = (EditText) findViewById(R.id.etTwitter);
        btnPost = (Button) findViewById(R.id.btnPost);
 
        // check if you are connected or not
        if(isConnected()){
            tvIsConnected.setBackgroundColor(0xFF00CC00);
            tvIsConnected.setText("You are conncted");
        }
        else{
            tvIsConnected.setText("You are NOT conncted");
        }
 
        // add click listener to Button "POST"
        btnPost.setOnClickListener(this);
 
    }
 
    public static String POST(String url, Person person){
        InputStream inputStream = null;
        String result = "";
        try {
 
            // 1. create HttpClient
            HttpClient httpclient = new DefaultHttpClient();
 
            // 2. make POST request to the given URL
            HttpPost httpPost = new HttpPost(url);
 
            String json = "";
 
            // 3. build jsonObject
            JSONObject jsonObject = new JSONObject();
            jsonObject.accumulate("name", person.getName());
            jsonObject.accumulate("country", person.getCountry());
            jsonObject.accumulate("twitter", person.getTwitter());
 
            // 4. convert JSONObject to JSON to String
            json = jsonObject.toString();
 
            // ** Alternative way to convert Person object to JSON string usin Jackson Lib
            // ObjectMapper mapper = new ObjectMapper();
            // json = mapper.writeValueAsString(person);
 
            // 5. set json to StringEntity
            StringEntity se = new StringEntity(json);
 
            // 6. set httpPost Entity
            httpPost.setEntity(se);
 
            // 7. Set some headers to inform server about the type of the content   
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");
 
            // 8. Execute POST request to the given URL
            HttpResponse httpResponse = httpclient.execute(httpPost);
 
            // 9. receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();
 
            // 10. convert inputstream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";
 
        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }
 
        // 11. return result
        return result;
    }
 
    public boolean isConnected(){
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected())
                return true;
            else
                return false;    
    }
    @Override
    public void onClick(View view) {
 
        switch(view.getId()){
            case R.id.btnPost:
                if(!validate())
                    Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show();
                // call AsynTask to perform network operation on separate thread
                new HttpAsyncTask().execute("http://hmkcode.appspot.com/jsonservlet");
            break;
        }
 
    }
    private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
 
            person = new Person();
            person.setName(etName.getText().toString());
            person.setCountry(etCountry.getText().toString());
            person.setTwitter(etTwitter.getText().toString());
 
            return POST(urls[0],person);
        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
       }
    }
 
    private boolean validate(){
        if(etName.getText().toString().trim().equals(""))
            return false;
        else if(etCountry.getText().toString().trim().equals(""))
            return false;
        else if(etTwitter.getText().toString().trim().equals(""))
            return false;
        else
            return true;    
    }
    private static String convertInputStreamToString(InputStream inputStream) throws IOException{
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;
 
        inputStream.close();
        return result;
 
    }   
}

Note: do not forget to add the permissions to AndroidManifest.xml file

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

( 5 ) Run & Check

  • Deploy the app and run it
  • Enter name, country & twitter
  • Click on “POST” button.
  • Wait for the “Data Sent!” message

Now, it is time to check that your data has been sent successfully to the server.