Android:アプリの中央にいる間に3GからWiFiスイッチ=ネットワーク接続の損失

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

質問

HTC Legend(Android 2.2)で迷惑な問題に遭遇しています。 Xperia、Galaxy、Nexusなどでこの問題は見られません。

3G接続でアプリを起動し、データを取得し、電話設定に移動してWiFiを有効にすると、電話は3Gよりも好まれるWiFi接続を自動的に取得します。問題は、アプリに戻ると、すべてのネットワーク接続を失い、何にも接続できないように見えることです。ただし、たとえば、Webブラウザなどの他のアプリは、新しいWiFi接続を使用して問題ありません。 pingは電話のシェルから正常に動作します。

十分に長く待つと(たとえば15分)、ネットワークスタックは自動的に修理するように見え、私のアプリはもう一度ネットワーク接続を行うことができます。もちろん、この遅延は受け入れられません。

プログラムでネットワークスタックを再インテッドする方法はありますか?毎回新しいjava.net.httpurlconnectionを作成しますが、wifiが取得されるとまだ時間がかかります。

ありがとう

コード:

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

および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)
役に立ちましたか?

解決

Sockettimeoutexceptionがあるように見えますが、おそらくこの例外をキャッチして新しいソケットを使用して接続できますか?

他のヒント

があります システムが古いHTTP接続を再利用する原因となるバグ。システムプロパティの設定http.Keepalive falseは問題を解決する必要があります。

System.setProperty("http.keepAlive", "false");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top