从WiFi连接到3G连接使挂开关? [英] Switching from WiFi connection to 3g causes connection to hang?

查看:132
本文介绍了从WiFi连接到3G连接使挂开关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到Web服务的应用程序。我可以连接到Web服务的次数没有任何问题,使用WIFI或3G,只要我在我的应用程序的生命周期内保持忠于我的连接类型。也就是说,如果我不从WIFI切换到3G。如果我从WIFI到3G的切换,我不能得到回应了。我的连接只是不断等待答复。

我试过低于4情景。我只是有与第3方案的问题。可能是什么问题?

第一个场景:连接总是在WIFI(OK)


  1. 应用程序使用WIFI连接到Web服务。

  2. 响应被成功接收。

  3. 应用程序使用WIFI再次连接到Web服务。

  4. 响应是成功接收。

第二方案:连接总是在3G(OK)


  1. 应用程序使用WIFI连接到Web服务。

  2. 响应被成功接收。

  3. 应用程序使用WIFI再次连接到Web服务。

  4. 响应是成功接收。

第三个方案:从WiFi连接切换到3G(无响应)


  1. 应用程序使用WIFI连接到Web服务。

  2. 响应被成功接收。

  3. 连接切换到3G。 WIFI是禁用的。 3G已启用。

  4. 应用程序再次连接到使用3G网络服务。

  5. 没有反应是或错误接收。应用不断等待答复。最后一个日志 GETRESPONSE code 调用之前显示。

第四场景:从3G连接切换到WIFI(OK)


  1. 应用程序连接到使用3G网络服务。

  2. 响应被成功接收。

  3. 连接切换到WIFI。 3G被禁用。 WIFI启用。

  4. 应用程序使用WIFI再次连接到Web服务。

  5. 响应是成功接收。

我的猜测是在默认情况下, HttpURLConnection类认为WIFI作为一个更好的连接类型比较3G。因此,当连接是从WIFI切换到3G, HttpURLConnection类拒绝承认3G,仍然尝试使用WIFI进行连接。在另一方面, HttpURLConnection类允许从3G切换到WiFi网络,因为WiFi是一个更好的连接类型。我是用这正确吗?如果是这样,我怎么允许从WIFI切换到3G?

下面是我的code的一个片段:(我称之为我每次连接到Web服务一次。)

  //打开新的连接
HttpsURLConnection的=(HttpURLConnection类)((新网址(URL))的openConnection());
httpsURLConnection.setDoInput(isDoInput);
httpsURLConnection.setDoOutput(isDoOutput);尝试
{
    //供应参数
    OutputStreamWriter WR =新OutputStreamWriter(httpsURLConnection.getOutputStream());
    wr.write(数据);
    wr.flush();    如果(HttpURLConnection类!= NULL)
    {
        如果(httpsURLConnection.getResponse code()== HttpsURLConnection.HTTP_OK)//连接挂在这里
        {
            //一些code
        }
        其他
        {
            //一些code
        }
    }}赶上(例外五)
{}


解决方案

我不知道为什么,但添加 System.setProperty(http.keepAlive,假)在code解决问题。

据Android开发者博客(http://android-developers.blogspot.ca/2011/09/androids-http-clients.html), HttpURLConnection类有错误的Froyo来之前,它可以通过禁用连接池,就像以上的解决。不过,我使用姜饼所以我不知道为什么HttpURLConnection类仍是行为不端在我的申请。

应用于其他:如果您可以提供更多的解释,请不要犹豫,编辑自己的帖子

I have an application which connects to a web service. I can connect to the web service any number of times without any problem using WIFI or 3G provided that I stay loyal to my connection type during the life cycle of my application. That is if I don't switch from WIFI to 3G. If I switches from WIFI to 3G, I can't get a response anymore. My connection just keeps on waiting for response.

I tried 4 scenarios below. I'm only having problem with the 3rd scenario. What could be the problem?

1st Scenario: Connection is always on WIFI (Ok)

  1. Application connects to a web service using WIFI.
  2. Response was received successfully.
  3. Application connects again to a web service using WIFI.
  4. Response was receive successfully.

2nd Scenario: Connection is always on 3G (Ok)

  1. Application connects to a web service using WIFI.
  2. Response was received successfully.
  3. Application connects again to a web service using WIFI.
  4. Response was receive successfully.

3rd Scenario: Connection switches from WIFI to 3G (No response)

  1. Application connects to a web service using WIFI.
  2. Response was received successfully.
  3. Connection was switched to 3G. WIFI is disabled. 3G is enabled.
  4. Application connects again to a web service using 3G.
  5. No was response or error was received. Application keeps on waiting for response. Last log was displayed before getResponseCode was called.

4th Scenario: Connection switches from 3G to WIFI (Ok)

  1. Application connects to a web service using 3G.
  2. Response was received successfully.
  3. Connection was switched to WIFI. 3G is disabled. WIFI is enabled.
  4. Application connects again to a web service using WIFI.
  5. Response was receive successfully.

My guess is by default, HttpURLConnection thinks WIFI as a better connection type compare to 3G. So when the connection is switched from WIFI to 3G, HttpURLConnection refuses to acknowledge 3G and still tries to connect using WIFI. On the other hand, HttpURLConnection allows switching from 3G to WIFI since WIFI is a better connection type. Am I correct with this? If so, how do I allow switching from WIFI to 3G?

Below is a snippet of my code: (I call it every time I connect to a web service.)

//open new connection
httpsURLConnection = (HttpURLConnection) ((new URL(url)).openConnection());
httpsURLConnection.setDoInput(isDoInput);
httpsURLConnection.setDoOutput(isDoOutput);

try
{
    //supply parameters
    OutputStreamWriter wr = new OutputStreamWriter(httpsURLConnection.getOutputStream());
    wr.write(data);
    wr.flush();

    if(httpURLConnection != null)
    {
        if (httpsURLConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) //connection hangs here
        {
            //some code
        }
        else
        {
            //some code
        }
    }

}catch(Exception e)
{

}

解决方案

I'm not sure why but adding System.setProperty("http.keepAlive", "false") on the code solves the problem.

According to the Android Developers Blog (http://android-developers.blogspot.ca/2011/09/androids-http-clients.html), HttpURLConnection has a bug prior to Froyo and it can be solved by disabling connection pooling just like above. However, I'm using Gingerbread so I'm not sure why HttpURLConnection is still misbehaving on my application.

To others: If you can provide more explanation, please don't hesitate to edit my post.

这篇关于从WiFi连接到3G连接使挂开关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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