java.net.UnknownHostException:无法解析主机“www.google.com”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26385748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.net.UnknownHostException: Unable to resolve host "www.google.com"
提问by Diego
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:
当连接到 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.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?
我在这里错过了什么?
回答by Seshu Vinay
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;
}
}