安卓:3G到WIFI开关,同时在应用程序的中间=网络连接丢失 [英] Android: 3G to WIFI switch while in the middle on the app = loss of network connectivity

查看:124
本文介绍了安卓:3G到WIFI开关,同时在应用程序的中间=网络连接丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个恼人的问题与HTC传奇(Android 2.2的)。没有看到有关的Xperia,银河,Nexus的,等这个问题。

当我发动对3G连接我的应用程序,获取一些数据,然后进入手机设置并启用WIFI,手机会自动获取被看好的3G WIFI连接。麻烦的是,有一次,我切换到该应用程序,它似乎已经失去了所有的网络connectivty并不能连接到任何东西。但是,其他应用程序,如Web浏览器,例如,必须使用新的无线连接没有问题。平安工作正常,从手机的外壳。

如果我等的时间够长,(如15分钟),网络堆栈似乎是自动自我修复和我的应用程序能够进行网络连接的一次。当然,这种延迟是不能接受的。

有没有办法重新初始化网络协议栈编程?我创建了一个新的java.net.HttpURLConnection中的每一次,但它仍然超时一旦WIFI已经收购了。

感谢

code:

 字节[]响应= NULL;
    HttpURLConnection的连接= NULL;
    INT响应code = -1;

    //首先检查缓存
    。字符串readyResponse = ResponseCache.getInstance()获取(的getURL());
    如果(readyResponse!= NULL){
        Log.d(LOG_TAG,返回缓存的服务器响应+的getURL());
        返回readyResponse.getBytes();
    }

    尝试 {

        网址URL =新的URL(的getURL());
        Log.i(LOG_TAG,发送请求:+ url.toExternalForm());

        连接=(HttpURLConnection类)url.openConnection();
        connection.setUseCaches(假);
        connection.setDoOutput(真正的);
        connection.setDoInput(真正的);
        connection.setConnectTimeout(ApplicationConfiguration.HTTP_CONNECT_TIMEOUT);
        connection.setReadTimeout(ApplicationConfiguration.HTTP_READ_TIMEOUT);

        如果(BuildType.getHTTPMethod()== BuildType.METHOD_GET)
        {
            connection.setRequestMethod(GET);
        }
        其他
        {
            connection.setRequestMethod(POST);
            connection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);

            串体= getParameters();
            connection.setRequestProperty(内容长度,Integer.toString(body.length()));

            OutputStreamWriter WR =新OutputStreamWriter(connection.getOutputStream());
            wr.write(getParameters());
            wr.flush();
        }



        connection.connect();

        响应code = connection.getResponse code();
 

和堆栈跟踪

  E / XXX.YYY.ZZZ(927):java.net.SocketTimeoutException:读超时
E / XXX.YYY.ZZZ(927):在org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativeread(本机方法)
E / XXX.YYY.ZZZ(927):在org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.access $ 200(OpenSSLSocketImpl.java:55)
E / XXX.YYY.ZZZ(927):在org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:532)
E / XXX.YYY.ZZZ(927):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readln(HttpURLConnectionImpl.java:1279)
E / XXX.YYY.ZZZ(927):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readServerResponse(HttpURLConnectionImpl.java:1351)
E / XXX.YYY.ZZZ(927):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.sendRequest(HttpURLConnectionImpl.java:1339)
E / XXX.YYY.ZZZ(927):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1656)
E / XXX.YYY.ZZZ(927):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
E / XXX.YYY.ZZZ(927):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponse$c$c(HttpURLConnectionImpl.java:1374)
E / XXX.YYY.ZZZ(927):在org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getResponse$c$c(HttpsURLConnectionImpl.java:117)
E / XXX.YYY.ZZZ(927):在xxx.yyy.zzz.executeRequest(zzz.java:95)
 

解决方案

我看到你有一个SocketTimeoutException如果,你或许可以捕获这个异常并连接使用一个新的socket?

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)

解决方案

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

这篇关于安卓:3G到WIFI开关,同时在应用程序的中间=网络连接丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆