Android:3G到WIFI切换,而在应用程序的中间=丢失网络连接
我遇到了一个恼人的问题与HTC Legend(Android 2.2)。在Xperia,Galaxy,Nexus等上没有看到这个问题。Android:3G到WIFI切换,而在应用程序的中间=丢失网络连接
当我在3G连接上启动我的应用程序时,获取一些数据,然后进入手机设置并启用WIFI,手机会自动获得一个有利的WIFI连接通过3G。麻烦的是,一旦我切换回应用程序,它似乎已经失去了所有的网络连接,无法连接任何东西。但是,其他应用程序,例如Web浏览器,使用新的Wifi连接没有问题。 Ping在手机外壳上工作良好。
如果我等待足够长的时间(例如15分钟),网络堆栈似乎会自动进行自我修复,我的应用程序可以再次进行网络连接。当然,这种延迟是不可接受的。
有没有办法以编程方式重新初始化网络堆栈?我每次创建一个新的java.net.HttpURLConnection,但是一旦WIFI被获取,它仍然会超时。
感谢
代码:
byte[] response = null;
HttpURLConnection connection = null;
int responseCode = -1;
// check the cache first
String readyResponse = ResponseCache.getInstance().get(getUrl());
if (readyResponse != null) {
Log.d(LOG_TAG, "Returning CACHED server response for " + getUrl());
return readyResponse.getBytes();
}
try {
URL url = new URL(getUrl());
Log.i(LOG_TAG, "Sending Request: " + url.toExternalForm());
connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(ApplicationConfiguration.HTTP_CONNECT_TIMEOUT);
connection.setReadTimeout(ApplicationConfiguration.HTTP_READ_TIMEOUT);
if (BuildType.getHTTPMethod() == BuildType.METHOD_GET)
{
connection.setRequestMethod("GET");
}
else
{
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String body = getParameters();
connection.setRequestProperty("Content-Length", Integer.toString(body.length()));
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(getParameters());
wr.flush();
}
connection.connect();
responseCode = connection.getResponseCode();
而且堆栈跟踪
E/xxx.yyy.zzz( 927): java.net.SocketTimeoutException: Read timed out
E/xxx.yyy.zzz( 927): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativeread(Native Method)
E/xxx.yyy.zzz( 927): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.access$200(OpenSSLSocketImpl.java:55)
E/xxx.yyy.zzz( 927): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:532)
E/xxx.yyy.zzz( 927): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readln(HttpURLConnectionImpl.java:1279)
E/xxx.yyy.zzz( 927): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readServerResponse(HttpURLConnectionImpl.java:1351)
E/xxx.yyy.zzz( 927): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.sendRequest(HttpURLConnectionImpl.java:1339)
E/xxx.yyy.zzz( 927): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1656)
E/xxx.yyy.zzz( 927): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
E/xxx.yyy.zzz( 927): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1374)
E/xxx.yyy.zzz( 927): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:117)
E/xxx.yyy.zzz( 927): at xxx.yyy.zzz.executeRequest(zzz.java:95)
我看到你有一个SocketTimeoutException,也许你可以捕获这个异常,并使用一个新的socket连接?
有一个bug that causes the system to reuse old http connections。 设置系统属性http.keepAlive为false应该解决的问题:
System.setProperty("http.keepAlive", "false");
只适用于pre-froyo – 2014-04-02 20:53:43
是的,我的解决方案是类似于您的建议,另外,我把setConnectTimeout()和setReadTimeout(),以非常低的值,只是不断敲打直到获得有效的套接字。 – Saideira 2011-03-03 20:03:02