【发布时间】:2018-01-26 08:32:09
【问题描述】:
我使用的是 Apache HttpClient 4.2,只需要从下面的 JSON 响应中获取 title 属性。
我需要为此使用 EntityUtils.toString() 方法吗?
代码
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(MAILCHIMP_API_URL);
postRequest.setHeader("Content-Type", "application/json");
postRequest.setHeader("Authorization", "Basic " + MAILCHIMP_API_KEY_BASE64);
StringEntity entity = new StringEntity(json.toString(), "UTF8");
postRequest.setEntity(entity);
HttpResponse response = httpClient.execute(postRequest);
// Closes the connection
EntityUtils.consume(response.getEntity());
JSON 响应
{
"type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title": "Member Exists",
"status": 400,
"detail": "user@domain.com is already a list member. Use PUT to insert or update list members.",
"instance": ""
}
【问题讨论】:
-
This 可能会有所帮助。
标签: java json http servlets apache-httpclient-4.x