且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HTTPGET的android不完全反应的BufferedReader

更新时间:2023-11-10 10:50:58

我已经在过去几天有完全一样的问题。我发现我的code工作通过WiFi而不是3G。换句话说,我消灭了所有常用的螺纹候选人。我还发现,当我跑了code在调试器,只是等待(说)client.execute(...)10秒后,它的工作。

I've had exactly the same issue for the last couple of days. I found that my code worked over WiFi but not 3G. In other words I eliminated all the usual threading candidates. I also found that when I ran the code in the debugger and just waited for (say) 10 seconds after client.execute(...) it worked.

我的猜测是,

response = httpclient.execute(httpget);

本身就是一个异步调用时,它的回报慢部分结果......因此,JSON反序列化出了问题。

is an asynchronous call in itself and when it's slow returns a partial result... hence JSON deserialization goes wrong.

相反,我试过这个版本有回调...

Instead I tried this version of execute with a callback...

try {
    BasicResponseHandler responseHandler = new BasicResponseHandler();
    String json = httpclient.execute(httpget, responseHandler);         
} finally {
    httpclient.close();
}

和突然间所有的工作。如果你不想要一个字符串,或者希望自己的code再看看在ResponseHandler的界面。希望有所帮助。

And suddenly it all works. If you don't want a string, or want your own code then have a look at the ResponseHandler interface. Hope that helps.