在 3g Android 上 httpget 后响应不完整
在我的 Android 应用程序中,我尝试使用 httpclient 和 httpget 获取网站。 它在模拟器和我的 HTC Desire HD 上运行良好。 但是当我断开 wifi 并尝试在 3G 网络上获取网页时,响应有时不完整。 我正在使用以下代码来获取网页:
public String htmlBody (String strURI)
{
String strBody = "";
HttpClient httpclient = new DefaultHttpClient();
//HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false);
try {
HttpGet httpget = new HttpGet(strURI);
HttpResponse response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
strBody = Functions.convertStreamToString(entity.getContent());
}
} finally {
httpclient.getConnectionManager().shutdown();
}
return strBody;
}
有没有办法确保响应完整?或者当响应不完整时恢复httpget?
in my Android application i am trying to get a website using the httpclient and the httpget.
it is working fine on the emulator and on my HTC Desire HD.
but when i disconnect from wifi and try to get te webpage on the 3G network the response is sometimes incomplete.
i am using the following code to get the webpage:
public String htmlBody (String strURI)
{
String strBody = "";
HttpClient httpclient = new DefaultHttpClient();
//HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false);
try {
HttpGet httpget = new HttpGet(strURI);
HttpResponse response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
strBody = Functions.convertStreamToString(entity.getContent());
}
} finally {
httpclient.getConnectionManager().shutdown();
}
return strBody;
}
is there a way to make sure that the response is complete? or resume the httpget when the response is incomplete?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近也遇到了和你同样的问题。经过几次实验,我想我找到了答案:
Android 不会在 3G 网络中通过单个 InputStream#read() 调用读取完整的响应。
以下代码可能有效:
I've encountered the same problem with you recently. After a few experiments, I guess I found the answer:
Android won't read complete response with a single InputStream#read() call in 3G network.
The following code may work:
只是一个想法:让您的网站在响应末尾添加一个特殊字符,然后检查该字符是否存在。如果它在那里,你就会知道你得到了完整的回复,如果它没有,那么重新发送请求或其他什么!
Just a thought: Make your website put like a special character at the end of the response and then check if that character exists. If its there you ll know u got the full response, if its not then resend the request or something!