【问题标题】:Why my HttpPost can't receive all response data?为什么我的 HttpPost 收不到所有响应数据?
【发布时间】:2010-05-08 02:18:38
【问题描述】:

我使用的是 Android 1.5,我的代码是这样的:

HttpPost httpPost = new HttpPost(url);
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost);
HttpEntity respEntity = response.getEntity();
String result = EntityUtils.toString(respEntity, DEFAULT_CHARSET);

成功执行这些代码后,结果是一个剥离的字符串。我已经尝试使用浏览器测试 url+param,它工作正常并获得了所有数据。

这段代码有什么问题?我需要指定任何参数吗?

【问题讨论】:

  • 如何剥离? DEFAULT_CHARSET 是否与响应正文的实际字符集匹配?
  • 是的,DEFAULT_CHARSET 与实际字符集匹配。我可以得到 2k 的内容并成功解码。但无法获取所有数据。
  • 对不起,我发现实际上响应中有完整的数据。这是因为 android.util.Log 有输出长度限制。所以我认为数据不完整。

标签: java android apache http


【解决方案1】:

尝试改用ResponseHandler pattern,看看是否能获得更好的结果。

【讨论】:

    【解决方案2】:

    这行得通:

    HttpResponse 响应=httpclient.execute(httppost); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多