POST Data in JSON format – ANDROID:

Step 1:

INternet Permission :

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

Step 2:

Initiate Asynktask.

new Save_task_to_server().execute();

Step 3 :

In Asynktask do the following steps:

public class Save_task_to_server extends
AsyncTask<String, Integer, Integer> {
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected Integer doInBackground(String… arg0) {
JSONObject obj = new JSONObject();
String username = “test_android”;
String password = “test”;
try {
//Put user name to JSONObject
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} obj.put(“username”, username);
try {
//Put password to JSONObject
obj.put(“password”, password);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray list = new JSONArray();
JSONObject tasks = new JSONObject();
try {
//Pu tasktype to JSONObject
tasks.put(“tasktype”, tasktype);

//Put timestamp to JSONObject
tasks.put(“timestamp”, timestamp);

//Put taskvalue to JSONObject
tasks.put(“taskvalue”, taskvalue);

//Put units consumed to JSONObject
tasks.put(“unitsconsumed”, unitsconsumed);

//Put JSONObject to JSONArray
list.put(tasks);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//Put JSONArray to JSONObject
obj.put(“tasks”, list);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}

HttpClient httpclient = new DefaultHttpClient();
//Post url
HttpPost httppost = new HttpPost(
“/*Enter your url here*/”);
httppost.setHeader(“Accept”, “application/json”);
httppost.setHeader(“Content-type”, “application/json”);
try {
//Add JSONObject to StringEntity
StringEntity se = new StringEntity(obj.toString());
httppost.setEntity(se);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (Exception e) {
e.printStackTrace();
}
HttpEntity response_entity = response.getEntity();
try {
Response = EntityUtils.toString(response_entity);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}

Expected Json POST object :

{“password”:”test”,”username”:”test_android”,”tasks”:[{“unitsconsumed”:”8686868″,”timestamp”:”2014-05-15 11:19:14″,”tasktype”:”inspection”,”taskvalue”:”20″},{“unitsconsumed”:”585″,”timestamp”:”2014-05-15 11:19:18″,”tasktype”:”inspection”,”taskvalue”:”3″},{“unitsconsumed”:”6868″,”timestamp”:”2014-05-15 11:19:41″,”tasktype”:”sequence”,”taskvalue”:”11″},{“unitsconsumed”:”9586″,”timestamp”:”2014-05-15 11:19:53″,”tasktype”:”sequence”,”taskvalue”:”11″}]}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s