1

I could't find any helpful tutorials on internet nor the documentation on developers site.

In my application i am connecting to a Web Server using HttpPost, when there is no internet connection, but wifi is on it shows a white screen and after some 10-15 secs "UnknownHostException".

I caught this Exception and made toast like

Unable to connect, check your internet connection.

and close the Activity (or the Application, since i am using finish() on the 1st Activity).

When the wifi itself is off i get an instant toast like"

You need internet connection to use this Application

but the 1st case is irritating. Taking 10-15 secs time and then showing the toast.

So i used HttpParameters and added a 5 sec ConnectionTimeout parameter.

But the application works same as before(no effect of this parameters).

How can i track if i hit ConnectionTime(5 secs over). So that i can show a Toast like

Slow internet connection

moreover why is the internet connection check not working when wifi is on but no internet

this is what i check when my application is lauched:

cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

if (isOnline(cm, this, SignUpActivity.this)){
//continue
}

public static boolean isOnline(ConnectivityManager cm, Context c, Activity a) {

    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }

    Toast.makeText(c, "You need internet access to run this application",
            Toast.LENGTH_SHORT).show();
    a.finish();
    return false;
}

am i only checking whether device's wifi is on. if so, how can i check whether i have internet connection, instead of just wifi

Thank You

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

1 Answers1

1

As for the first question, try using SocketTimeout instead.

As for the second question, the line

NetworkInfo netInfo = cm.getActiveNetworkInfo();

Will only get Wi-fi status (i.e. phone wifi antenna turned on) but not actual connectivity. The function returns immediately, so that if wifi is turned off you can toast out without checking connection further. But when wi-fi is turned on, you should go on and check your server's actual reachability, with something like

InetAddress.getByName(host).isReachable(timeOut)
Shine
  • 3,788
  • 1
  • 36
  • 59
  • Actually i am using SocketTimeout also. Sry i didn't mention it in the question. As for the 2nd solution, you mean i am just checking if wifi is on? I must change it o if wifi is on check if internet access is available? – Archie.bpgc Sep 28 '12 at 09:05
  • the second one. When wi-fi is on, and only in that case, check your actual server reachability – Shine Sep 28 '12 at 09:06
  • in my app this code works:HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = serverTimeout; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);HttpResponse response;response = httpclient.execute(httpget); – Shine Sep 28 '12 at 09:08
  • I actually followed this solution: http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available#answers-header where it says cm.getActiveNetworkInfo(); alone is enough to know if internet access is there – Archie.bpgc Sep 28 '12 at 09:09
  • that's not enough. The answer you posted says:"Note that having an active network interface doesn't guarantee that a particular networked service is available. Networks issues, server downtime, low signal, captive portals, content filters and the like can all prevent your app from reaching a server" – Shine Sep 28 '12 at 09:11
  • what do you mean by "in my app this code works". If you are unnable to connect to the server in time, what happens? – Archie.bpgc Sep 28 '12 at 09:11
  • it gives a ConnectionTimeoutException, as expected – Shine Sep 28 '12 at 09:12
  • note that, since your "network" code should be in a separate thread, for Toast to show you may have to use runOnUiThread(Runnable) – Shine Sep 28 '12 at 09:15
  • but my network code is not in a separate thread. I know i should do that, but will change later.Even here http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts#answers-header it says cm.getActiveNetworkInfo(); is enough. Do you know to checkif internet connection is available?? – Archie.bpgc Sep 28 '12 at 09:19
  • and usually in applications if a ConnectionTimeOutException Occurs. What will be the resultant action?? close the application??because you cannot say anything like "ConnectionTimeOut hence closing" as the user might want to wait for more time. – Archie.bpgc Sep 28 '12 at 10:23