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



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

  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)

我在这里错过了什么?

这个方法很容易做到这一点:

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

相关内容

  • 没有找到相关文章

最新更新