Android: 3G commutateur WIFI alors que dans le milieu sur l'application = perte de connectivité réseau

StackOverflow https://stackoverflow.com/questions/4347507

Question

Je suis en cours d'exécution dans un problème ennuyeux avec HTC Legend (Android 2.2). Ne voyant pas ce problème sur Xperia, Galaxy Nexus, etc.

Quand je lance mon application sur une connexion 3G, chercher des données, puis allez dans les paramètres du téléphone et activer le WiFi, le téléphone obtient automatiquement une connexion WIFI qui est favorisée par rapport à la 3G. Le problème est, une fois que je Revient à l'application, il semble avoir perdu tout connectivty réseau et incapable de se connecter à quoi que ce soit. Cependant, d'autres applications, comme le navigateur Web par exemple, ont aucun problème en utilisant la nouvelle connexion Wifi. Ping fonctionne très bien à partir de la coque du téléphone.

Si j'attendre assez longtemps, (par exemple 15 minutes), la pile réseau semble se réparer automatiquement et mon application est capable de faire des connexions réseau une fois de plus. Bien sûr, ce délai est inacceptable.

Y at-il un moyen de re-initialiser la pile réseau par programmation? Je crée une nouvelle java.net.HttpURLConnection à chaque fois, mais il encore temps une fois que le WIFI a été acquis.

Merci

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

Et 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)
Était-ce utile?

La solution

Je vois que vous avez un SocketTimeoutException, peut-être vous pouvez attraper cette exception et vous connecter en utilisant un nouveau socket?

Autres conseils

Il y a un bug qui cause le système de réutiliser vieux http connexions . Définition de la propriété du système http.keepAlive false devrait résoudre le problème:

System.setProperty("http.keepAlive", "false");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top