I am trying to check working internet connection (not wifi connected). I tried the below code to check if response is 200.
final int CONNECTION_TIMEOUT = 2000;
try
{
HttpURLConnection mURLConnection = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
mURLConnection.setRequestProperty("User-Agent", "ConnectionTest");
mURLConnection.setRequestProperty("Connection", "close");
mURLConnection.setConnectTimeout(CONNECTION_TIMEOUT);
mURLConnection.setReadTimeout(CONNECTION_TIMEOUT);
mURLConnection.connect();
return (mURLConnection.getResponseCode() == 200);
}
catch (IOException ioe)
{
Log.e("isInternetConnected", "Exception occured while checking for Internet connection: ", ioe);
}
return false;
I am calling the above method inside Async.
class checkInternet extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params)
{
internetavailable = isInternetConnected();
return null;
}
}
I get the correct results, but when there is no internet it is taking more than one minute to send me "NO INTERNET" information. Is this usual or is there a faster way to get the results?