0

In my Android Application, there are consistent and frequent server request and responses. If I launch the app with WiFi, then exit and relaunch with 3G. The request to Server fails with IO Exception.

Observations: This happens only on few WiFi ranges. It works perfectly fine at my home during WiFi-3G transition. Both the WiFi s (Home and Office) use WPA/WPA2 security protocols but exhibit different behaviout. I even tried to create a new socket when IO exception happend, but it did not help. But it works fine after 10-15 mins. App automatically connects to the right network and the app launches !!

What could be the issue here ?

Here is a snippet of code

public String readFromUrl(String urlString) {

    try {
        URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();

        BufferedReader in = new BufferedReader(new  InputStreamReader(urlConnection.getInputStream()));
        String line;
        while ((line = in.readLine()) != null)
            output.append(line);
        return output;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

It fails with exception :

java.net.SocketTimeoutException: Connection timed out
 W/System.err( 2615): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
 W/System.err( 2615): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
 W/System.err( 2615): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
 W/System.err( 2615): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
 W/System.err( 2615): at java.net.Socket.connect(Socket.java:1002)
 W/System.err( 2615): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
 W/System.err( 2615): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
dave.c
  • 10,910
  • 5
  • 39
  • 62
Sumanth
  • 31
  • 1
  • 4

1 Answers1

0

What is probably happening is that you have a connexion in progress when phone switches from 3G to Wifi. Phones don't know how to hand-over from 3G to WiFi today so the 3G connexion must be broken first hence the IOException

Philippe Girolami
  • 1,876
  • 1
  • 13
  • 15
  • Can you link any references that back up what you are saying please? And how exactly do you break the connection on switching network? Add a intent filter to handle ConnectivityManager broadcast when there are network changes and close the connection if that change is wifi connected? – ZeCodea May 21 '13 at 16:39