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());
}


我无法获取response。如何在Logger or console. 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());
}

07-28 01:50
查看更多