HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
try {
StringEntity documentStringified = new StringEntity(Obj.toString());
httpPost.setEntity(documentStringified);
} catch (UnsupportedEncodingException e) {
Log.d("UnsupportedEncodingException", e.toString());
}
try {
HttpResponse response = httpClient.execute(httpPost);
Log.d("Response", response.toString());
} catch (IOException e) {
Log.d("IOException", e.toString());
}
我无法获得响应
。如何在 Logger 或控制台中打印响应。
response.toString()
或 response.getEntity.toString()
不起作用。
我应该将 Content-type 设置为 "application/json"
吗?
最佳答案
一般的做法是:
String result = EntityUtils.toString(response.getEntity());
此外,您可以检查响应代码。有时,请求会失败。
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
String result = EntityUtils.toString(response.getEntity());
}
关于android - 获取 JSON 响应 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15704715/