Amt87
Amt87

Reputation: 5607

How to ping a website from an Android activity and get response?

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.

Upvotes: 3

Views: 17659

Answers (3)

Alireza Noorali
Alireza Noorali

Reputation: 3265

This Answer worked for me.

Don't forget to add internet permission:

<uses-permission android:name="android.permission.INTERNET" />

Upvotes: 0

Maher Abuthraa
Maher Abuthraa

Reputation: 17813

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

Upvotes: 6

Jay Vyas
Jay Vyas

Reputation: 2710

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

Upvotes: 4

Related Questions