无法解析主机“google。com”

时间:2020-12-23 15:54:01

I am running my application on a Galaxy Tab (v1).
I have added the

我正在运行我在Galaxy Tab (v1)上的应用程序。我已经添加了

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

permission.

许可。

I am checking if a site is online with the following code:

我正在检查一个网站是否有以下代码:

public boolean checkIfSite(final String url) {
    Data.isUp = false;

    new Thread(new Runnable() {
        public void run() {
            try {
                Data.isUp = InetAddress.getByName("google.com").isReachable(25000);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
    return Data.isUp;
}

I run it and it throws the exception:

我运行它,它抛出异常:

01-05 19:15:04.007: W/System.err(3303): java.net.UnknownHostException: Unable to resolve host "google.com": No address associated with hostname
01-05 19:15:04.007: W/System.err(3303):     at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
01-05 19:15:04.007: W/System.err(3303):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-05 19:15:04.007: W/System.err(3303):     at java.net.InetAddress.getByName(InetAddress.java:295)
01-05 19:15:04.007: W/System.err(3303):     at com.sonyericsson.extras.liveview.plugins.sandbox.SandboxPluginService$2.run(SandboxPluginService.java:184)
01-05 19:15:04.007: W/System.err(3303):     at java.lang.Thread.run(Thread.java:856)
01-05 19:15:04.007: W/System.err(3303): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
01-05 19:15:04.011: W/System.err(3303):     at libcore.io.Posix.getaddrinfo(Native Method)
01-05 19:15:04.011: W/System.err(3303):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
01-05 19:15:04.011: W/System.err(3303):     at java.net.InetAddress.lookupHostByName(InetAddress.java:411)
01-05 19:15:04.011: W/System.err(3303):     ... 4 more

1 个解决方案

#1


0  

Have you tried with http://google.com instead of just google.com?

你试过http://google.com而不是google.com吗?

And as @SLaks says, your method is wrong because you are returning after starting the Thread, need to use callbacks (similar to what you do with delegates in iOS).

正如@SLaks所说,您的方法是错误的,因为您在启动线程后返回,需要使用回调(类似于您在iOS中使用委托所做的事情)。

#1


0  

Have you tried with http://google.com instead of just google.com?

你试过http://google.com而不是google.com吗?

And as @SLaks says, your method is wrong because you are returning after starting the Thread, need to use callbacks (similar to what you do with delegates in iOS).

正如@SLaks所说,您的方法是错误的,因为您在启动线程后返回,需要使用回调(类似于您在iOS中使用委托所做的事情)。