javaandroidconnectivityhttpconnectionunknown-host

java.net.UnknownHostException: Unable to resolve host "www.google.com"


I'm trying to detect if there is actually an internet connection and websites are reachable. I have a broadcast receiver that runs the following method on receive:

  public boolean hasInternetNow() {
           Thread checkinternet = new Thread(new Runnable() {
               @Override
               public void run() {
                 try {
                      try {
                            ConnectivityManager cm = (ConnectivityManager) myContext.getSystemService(Context.CONNECTIVITY_SERVICE); 
                            if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
                                URL url = new URL("http://www.google.com");
                                HttpURLConnection urlc = (HttpURLConnection) url .openConnection();
                                urlc.setRequestProperty("User-Agent", "test");
                                urlc.setRequestProperty("Connection", "close"); 
                                urlc.setConnectTimeout(1000); // mTimeout is in seconds
                                urlc.connect();
                                if (urlc.getResponseCode() == 200) {
                                    setinternetDetected(true);
                                     } else {
                                    setinternetDetected(false);
                                  }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            setinternetDetected(false);
                        } catch (NullPointerException e){
                            e.printStackTrace();
                            setinternetDetected(false);
                        }


                 } catch (Exception e) {
                   e.getLocalizedMessage();
                   setinternetDetected(false);
                 }
               }
             });
            checkinternet.start();
            return internetDetected();
        }

While connected to the Wifi and google is reachable through the internet app, I get the following error:

10-15 07:50:46.656: W/System.err(24203): java.net.UnknownHostException: Unable to resolve host "www.google.com": No address associated with hostname
10-15 07:50:46.656: W/System.err(24203):    at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
10-15 07:50:46.656: W/System.err(24203):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-15 07:50:46.656: W/System.err(24203):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-15 07:50:46.656: W/System.err(24203):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
10-15 07:50:46.656: W/System.err(24203):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-15 07:50:46.656: W/System.err(24203):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-15 07:50:46.656: W/System.err(24203):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-15 07:50:46.656: W/System.err(24203):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-15 07:50:46.666: W/System.err(24203):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-15 07:50:46.666: W/System.err(24203):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
10-15 07:50:46.666: W/System.err(24203):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-15 07:50:46.666: W/System.err(24203):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-15 07:50:46.666: W/System.err(24203):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-15 07:50:46.666: W/System.err(24203):    at com.appiclife.ezcallcallingcardvoiptool.DeviceData$1.run(DeviceData.java:110)
10-15 07:50:46.666: W/System.err(24203):    at java.lang.Thread.run(Thread.java:856)
10-15 07:50:46.666: W/System.err(24203): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-15 07:50:46.666: W/System.err(24203):    at libcore.io.Posix.getaddrinfo(Native Method)
10-15 07:50:46.666: W/System.err(24203):    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
10-15 07:50:46.666: W/System.err(24203):    at java.net.InetAddress.lookupHostByName(InetAddress.java:411)

What am I missing here?


Solution

  • This method easily does the same:

    public boolean isInternetAvailable() {
            try {
                InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
    
                if (ipAddr.equals("")) {
                    return false;
                } else {
                    return true;
                }
    
            } catch (Exception e) {
                return false;
            }
    
        }