Switching from WiFi connection to 3g causes connection to hang?

一曲冷凌霜 提交于 2019-12-06 00:58:12
Arci

I'm not sure why but adding System.setProperty("http.keepAlive", "false") on the code solves the problem.

According to the Android Developers Blog (androids-http-clients), HttpURLConnection has a bug prior to Froyo and it can be solved by disabling connection pooling just like above. However, I'm using Gingerbread so I'm not sure why HttpURLConnection is still misbehaving on my application.

To others: If you can provide more explanation, please don't hesitate to edit my post.

when you make a connection on wifi and it switches back to 3G, do you see any connection drop exceptions?

From what i know, if you open a connection on wifi the same connection can't be used for 3G. you need to teardown existing connection and reestablish new connection on 3G.

I am guessing your problem could be because you haven't set any connection timeouts for your requests and as a result when connection drops it takes a long time for HttpUrlConnection to realize ur connection is lost.

I have put together utility libraries on http connections with some default timeouts, see if this can help you anyway

https://github.com/nareshsvs/android-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!