Android: 3G WIFI-Schalter, während in der Mitte auf dem app = Verlust der Netzwerkkonnektivität

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

Frage

Ich laufe in ein ärgerliches Problem mit HTC Legend (Android 2.2). Nicht zu sehen, dieses Problem auf Xperia, Galaxy Nexus, etc.

Wenn ich meine Anwendung auf einer 3G-Verbindung starten, einige Daten holen, geht dann in Telefoneinstellungen und ermöglicht WIFI, das Telefon erhält automatisch eine WIFI-Verbindung, die über 3G begünstigt wird. Das Problem ist, wenn ich an die App wechseln, scheint es, alle Netzwerk-connectivty verloren zu haben und nicht in der Lage, etwas zu verbinden. Aber auch andere Anwendungen, wie Web-Browser zum Beispiel kein Problem haben, die neue Wireless-LAN-Verbindung. Ping funktioniert gut von dem Shell-Telefon.

Wenn ich lange genug warten, (zum Beispiel 15 Minuten), scheint der Netzwerk-Stack selbst automatisch zu reparieren und meine App ist in der Lage Netzwerkverbindungen wieder zu machen. Natürlich ist diese Verzögerung nicht akzeptabel.

Gibt es eine Möglichkeit zum Re-init programmatisch den Netzwerk-Stack? Ich erstelle eine neue java.net.HttpURLConnection jedes Mal, aber es noch mal aus, sobald das WIFI erworben wurde.

Danke

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

Und 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)
War es hilfreich?

Lösung

Ich sehe Sie eine SocketTimeoutException haben, vielleicht können Sie diese Ausnahme abfangen und schließen Sie einen neuen Sockel mit?

Andere Tipps

Es gibt einen Fehler, der das System bewirkt, dass alte http wiederzuverwenden Verbindungen . Einstellen der Systemeigenschaft http.keepAlive auf false sollte das Problem beheben:

System.setProperty("http.keepAlive", "false");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top