4

An UnknownHostException is thrown if a server is down or if there is no internet connection. How can I determine if the UnknownHostException is thrown because the server is down or there is no internet connection?

The reason for this is I need to notify the user about the cause of the error. And I have to display something like this "Sorry. The service is currently not available. Please try again later" or "You do not have an internet connection".

Arci
  • 6,327
  • 20
  • 65
  • 95

3 Answers3

4

You could check the network state to determine if an internet connection is available (not 100% reliable though):

ConnectivityManager conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
    ||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING  ) {
...
}

EDIT :

IMHO, there is no way to tell for sure that the phone internet connectivity is down. But you could, in addition to checking the network state, make a simple test like downloading a small page from a website on the internet (pick some reliable server).

The result of this double test should be accurate 99% of the time.

Sébastien
  • 11,400
  • 8
  • 46
  • 63
  • 1
    Checking the network state like this is not a bullet proof way to check the availability of the internet connection. It is possible to for the internet connection to get disconnected right after doing the validation above. I'm looking for a way to know if the cause of UnknownHostException is due to lack of intenet connection or simply because the host is down. – Arci Apr 09 '12 at 12:09
  • Thanks! Currently, I am already doing a similar validation above to check the network connection of the device. However, as expected, it doesn't check the availability of the internet connection. Is connecting to a website, the only way to check for the availability of the internet connection? Is ethical to connect to a third party website like Google just for this sake even without their consent? Or is it not really necessary to ask for a consent on this one? – Arci Apr 11 '12 at 04:01
  • 1
    I don't think a company the size of Google would mind answering your "ping-like" queries (unless there's hundreds per seconds) ;) – Sébastien Apr 11 '12 at 08:08
2

UnKnownHostException is thrown to indicate that the IP address of a host could not be determined. It is not the case that no internet connection or server is down.

also check whether you had added internet permission in manifeast file.

Please refer this LINK

To check internet connection use bwlow method::

//To check whether network connection is available on device or not
public boolean checkInternetConnection(Activity _activity) {
    ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected())
        return true;
    else
        return false;
}//checkInternetConnection()

And if server is down mostly you will get Connection timeout Exception

Shankar Agarwal
  • 39,320
  • 8
  • 68
  • 66
  • +1 for the first part. You should indicate that `InetAddress.getByName()` method throws that. – Gray Apr 03 '12 at 12:39
  • Hi. Thanks for the answer. However, this does not answer my question. I know that UnknownHostException is thrown to indicate that the IP address of a host could not be determined. However, the reason for this could either be the host is down or there is no internet connection. Is there a way to know the reason why the Exception is thrown? UknownHostException is too general. – Arci Apr 09 '12 at 12:06
0

You can use the ConnectivityManager to check for network status.

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
bool hasConnectivity = connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting();
aromero
  • 24,833
  • 6
  • 49
  • 76