Android 如何通过HttpClient接收JSON对象?
我在android中使用HttpClient发送post请求:Android 如何通过HttpClient接收JSON对象?,android,json,jakarta-ee,request,httpclient,Android,Json,Jakarta Ee,Request,Httpclient,我在android中使用HttpClient发送post请求: HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(hostNameCollection); StringEntity se = new StringEntity(jsonObj.toString()); request.setHeader("Accept", "application/json
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(hostNameCollection);
StringEntity se = new StringEntity(jsonObj.toString());
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setEntity(se);
HttpResponse response = client.execute(request);
Log.v("HttpLogClient.logJSONObject", "wyslano JSON");
我不知道如何在Java EE servlet上接收JSON对象。您需要读取响应正文文本,然后解析为JSON
String result = EntityUtils.toString(response.getEntity());
JSONObject jo = new JSONObject(result);
读取http post(服务器端)的正文,方法是获取正文上的a流对象,然后读取它
一旦您阅读了它,将字节转换为字符,这将是json,您可以使用它使用“jackson”libs构建json对象,比如jsonNode 如果您使用的是普通servlet,则json流位于HttpServletRequest的主体中:request.getReader()或request.getInputStream() 为了使事情更简单,您可以使用一个库来处理数据绑定。 看看根森
YouClass object=new Genson()。反序列化(request.getReader(),YourClass.class);
//还是一张普通的地图
Map Map=genson.deserialize(request.getReader(),Map.class);
YouClass object = new Genson().deserialize(request.getReader(), YourClass.class);
// or to a plain map
Map<String, Object> map = genson.deserialize(request.getReader(), Map.class);