So I have a method that checks if the device is connected to the internet based on this one
It works ok, but in a couple of cases I find that although the phone shows being connected to the internet, it still does not have access to my server. What I am really looking to do it check connection to my specific URL.
How could I implement that?
A Mobile Web Server is software designed for modern-day smartphones to host personal web servers, through the use of open sourced software, such as, i-jetty, an open source software, based on jetty. I-jetty is an open source web container, serving Java-based web content such as, servlets and JSPs.
You could do something like:
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;
    }
}
This will attempt to connect to your server's URL and if it fails, you obviously don't have a connection. ;-)
You can try
InetAddress.getByName(host).isReachable(timeOut)
                        Runtime runtime = Runtime.getRuntime();
            Process proc;
            try {
                proc = runtime.exec("ping -c 1"+ (String) getText(R.string.Server));
                proc.waitFor();
                int exit = proc.exitValue();
                if (exit == 0) { // normal exit
                } else { // abnormal exit, so decide that the server is not
                            // reachable
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // other servers, for example
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With