我对应该是一个相当简单的任务有一些问题。我只需要将一个带有单个 JSON 对象的 JSON 数组发布到我的 Web 服务。整个 URL 请求需要这样格式化:
http://www.myserver.com/myservice.php?location_data=[{"key1":"val1","key2":"val2"....}]
try {
HttpClient httpclient = DefaultHttpClient();
HttpGet httpget = new HttpGet(URL+"?location_data="+JSONARRAY);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://www.myserver.com/myservice.php"
);
httpPost.setHeader("content-type", "application/json");
JSONObject locationData = new JSONObject();
locationData .put("key1", "val1");
locationData .put("key2", "val2");
StringEntity entity = new StringEntity(locationData.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);