[英]How to get a response to HTTPClient from a Servlet which is called from the HTTPClient?
[英]How to read the response from the HTTPClient
我正在围绕HTTPClient进行工作,以发送POST请求并获取响应。 我得到的响应代码是可以的。
我想从该响应中读取一些内容HttpEntity entity =response.getEntity();
我在控制台System.out.println(EntityUtils.toString(entity));
上打印该响应System.out.println(EntityUtils.toString(entity));
。
它正在打印{"created_at":"2011-12-01T07:56:50+00:00","type":"sample","id":29,"status":"received"}
我想从上面的字符串中读取id
。
任何人都可以帮助如何从实体中检索id。
提前致谢..
数据是JSON数据。
您可以转到json.org并下载JSONObject 。
你可以这样做,
JSONObject json = new JSONObject(EntityUtils.toString(entity));
int id = json.getInt("id");
看来你的回答是JSON之一。 你可以使用JSON库, 杰克逊吗?
这种方式不是很有效但你也可以试试。
BufferedReader br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
String readLine;
while(((readLine = br.readLine()) != null)) {
System.err.println(readLine);
}
if(readLine.indexOf(",") > -1 && readLine.indexOf(":") > -1)
String id = (readLine.split(",")[2]).split(":")[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.