Domanda

Sono in esecuzione in un problema fastidioso con HTC Legend (Android 2.2). Non vedendo questo problema su Xperia, Galaxy, Nexus, ecc.

Quando lancio il mio app su una connessione 3G, recuperare alcuni dati, poi andare in impostazioni del telefono e attivare Wi-Fi, il telefono ottiene automaticamente una connessione Wi-Fi che è favorita su reti 3G. Il problema è che, una volta che tornare alla app, sembra aver perso ogni connectivty di rete e in grado di connettersi a qualsiasi cosa. Tuttavia, altre applicazioni, come browser Web, per esempio, non hanno alcun problema utilizzando la nuova connessione Wifi. Ping funziona bene dalle coperture del telefono.

Se aspetto abbastanza a lungo, (ad esempio, 15 minuti), lo stack di rete sembra ripararsi automaticamente e la mia app è in grado di stabilire connessioni di rete, ancora una volta. Naturalmente, questo ritardo è inaccettabile.

C'è un modo per ri-init lo stack di rete a livello di codice? Creo un nuovo java.net.HttpURLConnection ogni volta, ma ancora una volta i tempi WIFI è stato acquisito.

Grazie

Codice:

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 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)
È stato utile?

Soluzione

Vedo che hai una SocketTimeoutException, forse si può prendere questa eccezione e connettersi utilizzando un nuovo socket?

Altri suggerimenti

C'è un bug che causa il sistema di riutilizzare vecchi http connessioni . L'impostazione della proprietà del sistema http.keepAlive false dovrebbe risolvere il problema:

System.setProperty("http.keepAlive", "false");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top