0

I got the following code from the intenet

      public boolean isConnectedToServer(String url, int timeout) {
    try {
        URL myUrl = new URL(url);
        URLConnection connection = myUrl.openConnection();
        connection.setConnectTimeout(timeout);
        connection.connect();
        return true;
    } catch (Exception e) {
        // Handle your exceptions
        return false;
    }
}

whenever i am calling this method :

    System.out.println(isConnectedToServer("192.168.1.65",5));

the output is still false and i can easily access my web service file so there is connectivity to this ip but i can't check if the ip is reachable, any thoughts ?

PS: using a local IP address just to connect to my XAMPP server

the Error:

    java.net.MalformedURLException: Protocol not found: 192.168.1.65
walidsarkis
  • 99
  • 1
  • 1
  • 11

1 Answers1

1

There is a problem with your ip. Try using http://ip... An URL is not an URI.

Reference: Protocol not found

See this code and look if it works:

Other example

Then you will have another problem. It is the thread you are working in. You are executing this code on the main thread, which generates an exception.

Your second problem: Network on main thread exception

Use an Async task to resolve this.

reference: Asynctask

Community
  • 1
  • 1
SamuelD
  • 333
  • 2
  • 10