的java.net.UnknownHostException:无法解析主机" www.google.com" [英] java.net.UnknownHostException: Unable to resolve host "www.google.com"

查看:2408
本文介绍了的java.net.UnknownHostException:无法解析主机" 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?

推荐答案

这个方法很容易地做同样的:

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

    }

这篇关于的java.net.UnknownHostException:无法解析主机&QUOT; www.google.com&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆