[{
"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?
[{
"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?
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();
....
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();