How to quickly check if URL server is available

后端 未结 8 2035
执念已碎
执念已碎 2020-12-09 16:30

I have a URL in the form

http://www.mywebsite.com/util/conv?a=1&from=%s&to=%s

And want to check if it is available.

The lin

8条回答
  •  独厮守ぢ
    2020-12-09 16:50

    here the writer suggests this:

    public boolean isOnline() {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
            int     exitValue = ipProcess.waitFor();
            return (exitValue == 0);
        } catch (IOException | InterruptedException e) { e.printStackTrace(); }
        return false;
    }
    

    Couldn’t I just ping my own page, which I want to request anyways?Sure! You could even check both, if you want to differentiate between “internet connection available” and your own servers beeing reachable

    read the link. its seems very good

    EDIT: in my exp of using it, it's not as fast as this method:

    public boolean isOnline() {
        NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnectedOrConnecting();
    }
    

    they are a bit different but in the functionality for just checking the connection to internet the first method may become slow due to the connection variables.

提交回复
热议问题