humansg
humansg

Reputation: 715

Android - Checking for internet connection (using open Wifi points that requires log in)

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?

Upvotes: 0

Views: 4623

Answers (3)

Benito Bertoli
Benito Bertoli

Reputation: 25793

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;
    }
}

Upvotes: 1

Maulik J
Maulik J

Reputation: 2765

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;
    }
}

Upvotes: 0

Newts
Newts

Reputation: 1372

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;

            } 

Upvotes: 0

Related Questions