0
我正在使用Apache HttpClient 4.2,只需從下面的JSON響應中獲取title
屬性。從Apache HttpClient中的HttpResponse對象獲取JSON屬性
我需要使用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": "[email protected] is already a list member. Use PUT to insert or update list members.",
"instance": ""
}
[This](https://stackoverflow.com/questions/10804466/how-to-convert-httpentity-into-json)可能會有幫助。 –