我遇到了HTC Legend(Android 2.2)的烦人问题。没有在Xperia,Galaxy,Nexus等上看到这个问题。
当我在3G连接上启动我的应用程序,获取一些数据,然后进入手机设置并启用WIFI,手机会自动获得一个比3G更受欢迎的WIFI连接。麻烦的是,一旦我切换回应用程序,它似乎已经失去了所有的网络连接,无法连接到任何东西。但是,其他应用程序(例如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();
和stacktrace
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)
答案 0 :(得分:1)
我看到你有一个SocketTimeoutException,也许你可以捕获这个异常并使用一个新套接字连接?
答案 1 :(得分:0)
有一个bug that causes the system to reuse old http connections。 将系统属性http.keepAlive设置为false应该可以解决问题:
System.setProperty("http.keepAlive", "false");