1
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()不起作用。

我应该将内容类型设置为"application/json".

4

2 回答 2

6

一般的做法是:

String result = EntityUtils.toString(response.getEntity());  

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

int status = response.getStatusLine().getStatusCode();
if (status == 200) {
    String result = EntityUtils.toString(response.getEntity());    
}
于 2013-03-29T13:57:56.543 回答
1

从响应中取出InputStream,然后使用 aScanner来使用它的内容。

String responseContent = new Scanner(inputStream).useDelimiter("\\A").next();

useDelimiter("\\A")表示“分隔符是流的结尾”,因此next()会消耗所有内容。

于 2013-03-29T13:57:24.273 回答