开发者

Incomplete response after httpget on 3g Android

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.getPa开发者_开发百科rams(), 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?


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;
}


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜