我在使用 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();
和堆栈跟踪
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)