我在使用 HTC Legend (Android 2.2) 时遇到了一个恼人的问题。在 Xperia、Galaxy、Nexus 等上没有看到此问题。
当我在 3G 连接上启动应用程序,获取一些数据,然后进入手机设置并启用 WIFI 时,手机会自动获取比 3G 更受青睐的 WIFI 连接。问题是,一旦我切换回应用程序,它似乎失去了所有网络连接并且无法连接到任何东西。但是,其他应用程序(例如 Web 浏览器)使用新的 Wifi 连接没有问题。 Ping 在手机外壳上运行良好。
如果我等待足够长的时间(例如 15 分钟),网络堆栈似乎会自动修复,并且我的应用程序能够再次建立网络连接。当然,这种拖延是不可接受的。
有没有办法以编程方式重新初始化网络堆栈?我每次都会创建一个新的java.net.HttpURLConnection,但一旦获取WIFI,它仍然超时。
Thanks
Code:
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,也许您可以捕获此异常并使用新套接字进行连接?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)