获取JSON响应Android

Kev*_*vin 1 android json

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());
}
Run Code Online (Sandbox Code Playgroud)

我无法得到response.如何打印响应Logger or console. response.toString()response.getEntity.toString()不起作用.

我应该将Content-type设置为"application/json".

Ost*_*siv 6

一般方法是:

String result = EntityUtils.toString(response.getEntity());  
Run Code Online (Sandbox Code Playgroud)

此外,您可以检查响应代码.有时,请求失败.

int status = response.getStatusLine().getStatusCode();
if (status == 200) {
    String result = EntityUtils.toString(response.getEntity());    
}
Run Code Online (Sandbox Code Playgroud)