19

I have an Android application which connects to the Internet. I am trapping all the possible scenarios for the connection and notice that when I don't have an Internet connection, an UnknownHostException is thrown. I am a bit confused here since getting an UnknownHostException will mean that the application was able to connect to the Internet but wasn't able to find the given URL.

Am I getting the right Exception? Could you explain why am I getting an UnknownHostException in this?

Also, can you tell the specific Exceptions for these scenarios:

  • When there is no Internet connection.
  • When the URL cannot be found.
  • When the request timed out.
  • When the website is down.
  • When access is denied.

I would also appreciate it if you could give me more scenarios and Exceptions. I have to trap all the possible connections and display the most appropriate message depending on the type of connection Error.

Vivek Kalkur
  • 2,200
  • 2
  • 21
  • 40
Arci
  • 6,647
  • 20
  • 70
  • 98

4 Answers4

35

getting an UnknownHostException will mean that the application was able to connect to the Internet

No it doesn't. It means the application was unable to resolve the host name. That could be because the host name doesn't exist, or because it was unable to connect to the Internet to resolve it.

When there is no Internet connection.

No specific exception. "There is no Internet connection" doesn't have a well-defined meaning. The condition resolves to one of the other failure modes below.

When the URL cannot be found.

If the host cannot be found, UnknownHostException. If the content part of the URL cannot be found, HTTP 404.

When the request timed out.

ConnectException with 'connection timed out' as the message, or SocketTimeoutException if it's a read timeout.

When the website is down.

ConnectException with 'connection refused' as the message.

When access is denied.

HTTP 403.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    Thank you! So there is no way to determine if an exception is due to a lack of internet connection? – Arci Dec 20 '11 at 15:49
  • I've already answered that. The condition 'lack of an Internet connection' itself doesn't have a well-defined meaning, but it resolves to one of the three exceptions I listed when I said so. – user207421 Aug 29 '13 at 05:52
3

Checking Internet Connection,Just try this sample function....

public static boolean CheckInternet(Context context) 
{
    ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    return wifi.isConnected() || mobile.isConnected();
}

I hope this help....

oefe
  • 19,298
  • 7
  • 47
  • 66
Uttam
  • 12,361
  • 3
  • 33
  • 30
  • 1
    There is no point in testing `mobile.isConnected()` twice, etc. All this can be reduced to one line: `return wifi.isConnected() || mobile.isConnected()`. – user207421 Dec 20 '11 at 09:57
  • Thanks! I'm also planning to implement a similar checking method if the exception thrown won't be enough to determine if there is no internet connection. – Arci Dec 20 '11 at 15:54
  • 1
    This is not helpful when your device has usb passthrough connection. – Sushovan Mukherjee Feb 12 '16 at 11:02
2

for checking internet connectivity ....

boolean b_IsConnect = isNetworkAvailable();
private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
1

To check internet connection, use this function .

  public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) 
              for (int i = 0; i < info.length; i++) 
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }
      }
      return false;
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Rizvan
  • 2,335
  • 2
  • 23
  • 42