问题
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)
- Application connects to a web service using WIFI.
- Response was received successfully.
- Application connects again to a web service using WIFI.
- Response was receive successfully.
2nd Scenario: Connection is always on 3G (Ok)
- Application connects to a web service using WIFI.
- Response was received successfully.
- Application connects again to a web service using WIFI.
- Response was receive successfully.
3rd Scenario: Connection switches from WIFI to 3G (No response)
- Application connects to a web service using WIFI.
- Response was received successfully.
- Connection was switched to 3G. WIFI is disabled. 3G is enabled.
- Application connects again to a web service using 3G.
- 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)
- Application connects to a web service using 3G.
- Response was received successfully.
- Connection was switched to WIFI. 3G is disabled. WIFI is enabled.
- Application connects again to a web service using WIFI.
- 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)
{
}
回答1:
I'm not sure why but adding System.setProperty("http.keepAlive", "false")
on the code solves the problem.
According to the Android Developers Blog (androids-http-clients), 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.
回答2:
when you make a connection on wifi and it switches back to 3G, do you see any connection drop exceptions?
From what i know, if you open a connection on wifi the same connection can't be used for 3G. you need to teardown existing connection and reestablish new connection on 3G.
I am guessing your problem could be because you haven't set any connection timeouts for your requests and as a result when connection drops it takes a long time for HttpUrlConnection to realize ur connection is lost.
I have put together utility libraries on http connections with some default timeouts, see if this can help you anyway
https://github.com/nareshsvs/android-libraries
来源:https://stackoverflow.com/questions/10242942/switching-from-wifi-connection-to-3g-causes-connection-to-hang