android - 获取 JSON 响应 Android

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

我无法获得响应。如何在 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/

相关文章:

java - 如何将单选按钮的部分描述设置为粗体?

java - 如何使用Android手机获取系统的IP地址?

android - 如何使用 Espresso 测试获取 Android 项目的覆盖率

c# - 我如何使用 utc +0600 将 json 日期解析为 c# 日期时间格式

jquery - 使用jquery获取json对象中的元素数量

android - 在 Android 中支持针对相应 API 级别的相同 View 的不同外观

android - 如何替换 xml 项目的可绘制源

javascript - jsrender-从复杂结构中访问元素

javascript - 如何提取正文请求 node.js

java - 接收 JSON 并将其转换为对象的最佳实践