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

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

10 Comments

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?
the second one. When wi-fi is on, and only in that case, check your actual server reachability
in my app this code works:HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = serverTimeout; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);HttpResponse response;response = httpclient.execute(httpget);
I actually followed this solution: stackoverflow.com/questions/4238921/… where it says cm.getActiveNetworkInfo(); alone is enough to know if internet access is there
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"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.