Pergunta

I am running into a annoying problem with HTC Legend (Android 2.2). Not seeing this issue on Xperia, Galaxy, Nexus, etc.

When I launch my app on a 3G connection, fetch some data, then go into phone Settings and enable WIFI, the phone automatically obtains a WIFI connection which is favoured over 3G. The trouble is, once i switch back to the app, it appears to have lost all network connectivty and unable to connect to anything. However, other apps, like Web Browser for example, have no problem using the new Wifi connection. Ping works fine from the phone's shell.

If I wait long enough, (e.g. 15 minutes), the network stack seems to repair itself automatically and my app is able to make network connections once again. Of course, this delay is unacceptable.

Is there a way to re-init the network stack programmatically? I create a new java.net.HttpURLConnection each time, yet it still times out once the WIFI has been acquired.

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();

And 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)
Foi útil?

Solução

I see you have a SocketTimeoutException, perhaps you can catch this exception and connect using a new socket?

Outras dicas

There is a bug that causes the system to reuse old http connections. Setting the system property http.keepAlive to false should resolve the problem:

System.setProperty("http.keepAlive", "false");
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top