由于您的 Web 服务在请求中期望 JSONObject,您可以使用setEntity()
.
例如:
JSONObject objRequest = new JSONObject();
objRequest.put("Apikey","abcdefgh-ijkl-mnop-qrst-uvwxyz12345");
objRequest.put("Imei","0123456789012354");
JSONObject objGps = new JSONObject();
objGps.put("Latitude",1.23);
objGps.put("Longitude",4.56);
objRequest.put(Gps, objGps);
现在,这是一种使用请求数据调用 Web 服务的方法:
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost= new HttpPost(url);
post.addHeader("Content-Type", "application/json; charset=utf-8"); // addHeader()
httpPost.setEntity(new StringEntity(objRequest.toString(),"utf-8")); // request data
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}