java.net.UnknownHostException:无法解析主机“www.google.com”

时间:2014-10-15 14:59:28

标签: java android connectivity httpconnection unknown-host

我正在尝试检测是否确实存在互联网连接并且网站可以访问。我有一个广播接收器,在接收时运行以下方法:

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

虽然通过互联网应用程序连接到Wifi和谷歌,但我收到以下错误:

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)

我在这里缺少什么?

2 个答案:

答案 0 :(得分:7)

这种方法很容易做到:

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

    }

答案 1 :(得分:-1)

您的设备似乎已连接到互联网,但是互联网不可用。请进行连接,然后使用google.com域重试。希望对您有帮助