【问题标题】:Incomplete http response in androidandroid中的http响应不完整
【发布时间】:2014-02-19 13:08:17
【问题描述】:

我正在尝试从我的办公室服务器获得响应。响应至少有 50,000 行。但我没有得到完整的回应。我不知道哪里出错了。是否完整的数据没有下载或我的 InputStream 有任何其他问题?

HttpClient httpClient = new DefaultHttpClient();
HttpContext httpcontext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(mUrl);
HttpResponse mResponse = httpClient.execute(httpPost, httpcontext);
InputStream mResponseStream = mResponse.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(mResponseStream));
String line = "";
while ((line = br.readLine()) != null) {
    line = line.replaceAll("[\r\n]", "");
    Log.v("Response", line);
}

必填回复:

02-17 18:32:14.000: D/Lib(4121): Starts
02-17 18:32:14.010: D/Lib(4121): 12080964|~|1370.00
02-17 18:32:14.010: D/Lib(4121): 12083142|~|500.00
02-17 18:32:14.020: D/Lib(4121): 13051372|~|100.00
02-17 18:32:14.020: D/Lib(4121): 13040457|~|12.00
02-17 18:32:14.060: D/Lib(4121): 12010037|~|170.00
02-17 18:32:14.060: D/Lib(4121): .........
02-17 18:32:14.060: D/Lib(4121): Ends

得到响应:

02-17 18:32:14.000: D/Lib(4121): Starts
02-17 18:32:14.010: D/Lib(4121): 12080964|~|1370.00
02-17 18:32:14.010: D/Lib(4121): 12083142|~|500.00
02-17 18:32:14.020: D/Lib(4121): 13051372|~|100.00
02-17 18:32:14.020: D/Lib(4121): 13040457|~|12.00
02-17 18:32:14.060: D/Lib(4121): 12010037|~|170.00
02-17 18:32:14.060: D/Lib(4121): .........

我的回复总是以Starts 关键字开头,以Ends 关键字结尾。总是我没有得到Ends 关键字。响应在第 25,000 到 33,000 行停止。谢谢。

【问题讨论】:

  • 当您手动请求或通过 curl 等其他工具请求时会得到什么? (顺便说一句:你应该做 GET 请求,而不是 POST - 你发送任何需要 POST 的东西)
  • 尝试为 BufferedReader 分配大小
  • @MarcinOrlowski 我正在发送数据进行身份验证,但我没有使用 curl。
  • @SathishKumar 使用 curl 看看那里的输出是什么
  • @MarcinOrlowski 抱歉,我不知道 curl,

标签: android


【解决方案1】:

在这里我从响应中获取字符串,然后我将该字符串响应转换为InputStream,这个过程给出了完整的响应。当我尝试从响应中获取 InputStream 时,它返回不完整的数据。无论如何,我得到了完整的回复,但直到我不知道为什么我无法使用 mResponse.getEntity().getContent() 得到完整的回复。

我的工作代码,

HttpClient httpClient = new DefaultHttpClient();
HttpContext httpcontext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(mUrl);
HttpResponse mResponse = httpClient.execute(httpPost, httpcontext);
String responseString=EntityUtils.toString(mResponse.getEntity());
InputStream mResponseStream = new ByteArrayInputStream(responseString.getBytes(HTTP.UTF_8));
BufferedReader br = new BufferedReader(new InputStreamReader(mResponseStream));
String line = "";
while ((line = br.readLine()) != null) {
    line = line.replaceAll("[\r\n]", "");
    Log.v("Response", line);
}

【讨论】:

    猜你喜欢
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多