Connection test

Asked

Viewed 286 times

0

I have an application that will send some packages to the server.

I would like to before sending, check if the server is available. And for that, I did the following:

   public static boolean isConnect(){

        boolean isOn = false;
        try{
            final String command = "/system/bin/ping -c 1 "+HOST;
            int wait = Runtime.getRuntime().exec(command).waitFor();
            Log.d(TAG,"wait: "+wait);
            isOn = ( wait == 0);
        }catch (final Exception e){}
        Log.d(TAG,"Connect: "+isOn);
        return isOn;
    }

But in all cases it always returns 1.

According to the documentation:

the Exit value of the Native process being waited on.

This may vary if I am using 3G?

Or how best to take this test?

I do not care if I have internet connection, but if the HOST is responding!

  • As in "This may vary, if I am using 3g?"

  • 1

    Simply do what you have to do and fix the error. There is no advantage to doing this test every time you need to access your host. He has an obligation to always be available.

  • @Paulohdsousa , being the 3g longer, the return time between 3g and Wifi can be different?

1 answer

0


Actually the method works correctly, the problem is that the HOST does not accept ping!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.