2

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

<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
Bart
  • 19,692
  • 7
  • 68
  • 77
Allison
  • 2,213
  • 4
  • 32
  • 56
  • 4
    You're method is fundamentally broken; you're returning the value before the thread runs. If you wait for the thread, you'll freeze your app, which is exactly what you should avoid. You need to use callbacks or promises. – SLaks Jan 06 '13 at 02:35
  • I'm returning it *before* it runs? I `.start()` it and then after it's down it returns – Allison Jan 06 '13 at 16:54
  • No. You `.start()` and then you return _immediately_. – SLaks Jan 08 '13 at 02:46
  • There's been someone else with somewhat similar problem; http://stackoverflow.com/questions/2096874/inetaddress-getbyname-on-android – harism Jan 12 '13 at 21:26

1 Answers1

0

Have you tried with http://google.com instead of just 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).

momo
  • 3,404
  • 6
  • 37
  • 66