0
[{
 "Name":"abc"
 "Place":"def"

}]

The above is the Json array. How to post a Json array to webserver in Android? Is there any sample code available?

halfer
  • 19,824
  • 17
  • 99
  • 186
Android_programmer_camera
  • 13,197
  • 21
  • 67
  • 81
  • Did you try anything? for example http://developer.android.com/reference/org/apache/http/client/methods/HttpPost.html – Mario Stoilov Dec 04 '13 at 19:17
  • What do you mean ? Send this array as string (the string that you put as code in your question) in a POST http var to a web server ? –  Dec 04 '13 at 19:17
  • Hi gahfy ! Can I convert this array as String ,add as parameter and set it to "setEntity()" method and post – Android_programmer_camera Dec 04 '13 at 19:19
  • yes, you can >> http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ – Mario Stoilov Dec 04 '13 at 19:23
  • But how is your json array in your java code ? May you add samples of your JSON code ? –  Dec 04 '13 at 19:25
  • Hi gahfy ! my jsonArray code is as below:JSONArray finalArray= new JSONArray(); JSONObject Details = new JSONObject(); Details.put("Name" , "abc"); Details.put("Place" , "bdc"); finalArray.put(Details ); Gahfy ! please help me in sorting out this issue? – Android_programmer_camera Dec 04 '13 at 19:32
  • Here is another link to help you http://stackoverflow.com/questions/7181534/http-post-using-json-in-java – bobby.dhillon Dec 04 '13 at 19:33

2 Answers2

1

You could try something like that:

1) Build the JSON object

JSONObject params = new JSONObject();
params.put("Name", name);
params.put("Place", place);

2) Post to server

private HttpURLConnection connection;
connection = (HttpURLConnection) <your_url>.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestMethod("POST");

byte[] outputBytes = <your_json_object>.toString().getBytes("UTF-8");
OutputStream os = connection.getOutputStream();
os.write(outputBytes);
os.close();
....
mbmc
  • 5,024
  • 5
  • 25
  • 53
0

You can also try using HttpPost request and string entity to send JSON use following code:

HttpClient client = new DefaultHttpClient(httpParameters);

HttpUriRequest request;

request = new HttpPost(url);

    StringEntity entity = new StringEntity(**<-Your JSON->**);
    ((HttpPost) request).setEntity(entity);


((HttpPost) request).setHeader("Content-Type",
"application/json");

HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
vipul mittal
  • 17,343
  • 3
  • 41
  • 44