0

I followed this http://www.helloandroid.com/tutorials/it-internet-connection-checker-snippet to check if there is internet connection on my android device.

The code block looks like this

URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(1000 * 5); // Timeout is in seconds
urlc.connect();
if (urlc.getResponseCode() == 200) {
    // http response is OK
    Log.d("db", "Connection to " + url.toString() + " successful!");
    return true;
}

However, my device is connected to open Wifi points which requires webpage log in before accessing the internet. This code seems to return true even without log in.

Was wondering what I can do?

humansg
  • 735
  • 3
  • 12
  • 30
  • did you want to check wifi or internet conncetion is available – Newts Feb 01 '12 at 10:16
  • What is the post url form you are submitting to ? – Zakaria Feb 01 '12 at 10:28
  • actually i am following the [link](http://www.helloandroid.com/tutorials/it-internet-connection-checker-snippet) I want to do something like a "ping" to confirm that there is actually an internet connection and not just a network(wifi/3g) connection. Wifi and 3G connection is not sufficient to determine that I am connected to the internet. – humansg Feb 01 '12 at 11:57

3 Answers3

1

If you have your own domain, create a web page that contains only one keyword of your choice. For example: "success"

Now connect to that page instead of google and check if it returns "success".

URL url = new URL("http://www.yourdomain.com/yourpage.html");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(1000 * 5); // Timeout is in seconds
urlc.connect();
if (urlc.getResponseCode() == 200) {
    // http response is OK

    InputStream in = urlc.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String line = reader.readLine();
    in.close();
    if (line == "success"){
        Log.d("db", "Connection to " + url.toString() + " successful!");
        return true;
    } else {
        return false;
    }
}
Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
  • I do not have my own web domain. I am using this code as I thought it is something like a "ping" which will determine that my connection to that site is available... – humansg Feb 01 '12 at 11:59
0

I think you have to use this method to check internet connection i available.

public boolean isOnline()
        {

             ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
             NetworkInfo ni = cm.getActiveNetworkInfo();
             boolean result = false;
             if(ni != null )
             {
                 if(  ni.getState() == NetworkInfo.State.CONNECTED )
                 {
                     result = true;
                 }
             }

             return result;

            } 
Newts
  • 1,354
  • 14
  • 23
  • I thought this line of code only checks if you are connected to a network? ie: Wifi / 3G. It does not check if that internet connection is alive? – humansg Feb 01 '12 at 11:55
  • I can check if wifi is on and internet exist but how to check if wifi is enabled but internet connection is not there. Please help – Prax Aug 23 '12 at 11:45
0
private boolean checkInternetConnection() {
    ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {
        Toast toast = Toast
                .makeText(
                        getApplicationContext(),
                        "No Internet Connection Found !!! Please Connect To Internet First",
                        Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(5000);
                    finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return false;
    }
}
Maulik J
  • 2,745
  • 19
  • 22