ΩlostA
ΩlostA

Reputation: 2601

I need to check synchronously if an url of server is working, it returns false everytime error: android.os.NetworkOnMainThreadException

When I want to reach a server, url or website, my function returns false everytime. I want a function that indicates me the code status 200, the function must wait I think to give me a true or a false... I don't want async solutions.

 private boolean isServerReachable() {
        try {
            InetAddress.getByName("http://www.google.com").isReachable(2000); 
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

Any ideas ? I based my code on that ticket that is popular: Ticket 1 Thanks in advance

Upvotes: -4

Views: 77

Answers (1)

chau nguyen ngoc
chau nguyen ngoc

Reputation: 9

You can change "http://www.google.com" to google.com this parameter is host name.

Upvotes: -1

Related Questions