在 3g Android 上 httpget 后响应不完整

发布于 2024-10-29 15:08:30 字数 735 浏览 6 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初与友歌 2024-11-05 15:08:30

我最近也遇到了和你同样的问题。经过几次实验,我想我找到了答案:
Android 不会在 3G 网络中通过单个 InputStream#read() 调用读取完整的响应。
以下代码可能有效:

InputStream in = entity.getContent();
int length = 0;
while (true) {
    int ret = in.read(buffer, length, buffer.length - length);
    if (ret == -1) break;
    length += ret;
}

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:

InputStream in = entity.getContent();
int length = 0;
while (true) {
    int ret = in.read(buffer, length, buffer.length - length);
    if (ret == -1) break;
    length += ret;
}
战皆罪 2024-11-05 15:08:30

只是一个想法:让您的网站在响应末尾添加一个特殊字符,然后检查该字符是否存在。如果它在那里,你就会知道你得到了完整的回复,如果它没有,那么重新发送请求或其他什么!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文