3

I used isReachable, but it didn't work, and I used ConnectivityManager and getNetworkInfo; actually this works only to check if I am connected to the Internet...

But the issue is I want to check if I can access the Internet so I want to ping a website to check if there is a response.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amt87
  • 5,493
  • 4
  • 32
  • 52

3 Answers3

6

For the get method:

private void executeReq(URL urlObject) throws IOException{
    HttpURLConnection conn = null;

    conn = (HttpURLConnection) urlObject.openConnection();
    conn.setReadTimeout(100000); //Milliseconds
    conn.setConnectTimeout(150000); //Milliseconds
    conn.setRequestMethod("GET");
    conn.setDoInput(true);

    // Start connect
    conn.connect();
    String response = convertStreamToString(conn.getInputStream());
    Log.d("Response:", response);
}

You may call it with

try {
    String parameters = ""; //
    URL url = new URL("http://alefon.com" + parameters);
    executeReq(url);
}
catch(Exception e){
    //Error
}

To check Internet connectivity, use:

private void checkInternetConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (null == ni)
        Toast.makeText(this, "no internet connection", Toast.LENGTH_LONG).show();
    else {
         Toast.makeText(this, "Internet Connect is detected .. check access to sire", Toast.LENGTH_LONG).show();
         //Use the code above...
    }
}
Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
  • Thank you @Mohammed, the problem is that I want to check if I can access any website if yes let a Toast says "There is internet access" ... and if not the Toast says "Browsing problem" even though the device is connected to wifi – Amt87 Mar 26 '12 at 14:47
  • I update the answer .. check it plz .. but its still necessary to check access to website .. because may server go down, or no access etc.. so check internet connection then check call site and get response.... – Maher Abuthraa Mar 26 '12 at 15:09
4

Use this one.. This works fine for me :)

public static void isNetworkAvailable(Context context){
    HttpGet httpGet = new HttpGet("http://www.google.com");
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    try{
        Log.e("checking", "Checking network connection...");
        httpClient.execute(httpGet);
        Log.e("checking", "Connection OK");
        return;
    }
    catch(ClientProtocolException e){
        e.printStackTrace();
    }
    catch(IOException e){
        e.printStackTrace();
    }

    Log.e("checking", "Connection unavailable");
}
Jay Vyas
  • 2,674
  • 5
  • 27
  • 58
0

This Answer worked for me.

Don't forget to add internet permission:

<uses-permission android:name="android.permission.INTERNET" />
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80