Android Java 用 ping 判断是否可以连接主机

    // 奇怪的是,ping在Android中不可用,线程异常退出(exitValue非零),纳闷...
    // Unfortunately, ping doesn't work in Android !
    @Deprecated
    public static boolean ping(String address, String argStr, long timeout, TimeUnit unit) {
    
    
        String host;
        try {
    
    
            host = new WebAddress(address).getHost();
        } catch (ParseException e){
    
    
            Log.w(TAG, "address " + address + " is invalid!");
            return false;
        }
        String command = "ping " + argStr + " " + host;
        Process pingProcess;
        boolean isTimeout;
        try {
    
    
            pingProcess = Runtime.getRuntime().exec(command);
            isTimeout = !pingProcess.waitFor(timeout, unit);
        } catch (IOException | InterruptedException e) {
    
    
            Log.w(TAG, "Exec \"" + command + "\" exception", e);
            return false;
        }
        String logPrefix = "--- " + command + " --- ";
        if (isTimeout) {
    
    
            Log.w(TAG, logPrefix + "timeout!");
            return false;
        } else if (pingProcess.exitValue() != 0) {
    
    
            Log.w(TAG, logPrefix + "exited abnormally!");
            return false;
        } else {
    
    
            StringBuilder outputSb = new StringBuilder();
            try (InputStream input = pingProcess.getInputStream();
                 BufferedReader br = new BufferedReader(new InputStreamReader(input))) {
    
    
                String line = null;
                while ((line = br.readLine()) != null) {
    
    
                    outputSb.append('\n');
                    outputSb.append(line);
                }
            } catch (IOException e) {
    
    
                Log.w(TAG, "ping read output exception", e);
            }
            Log.d(TAG, logPrefix + "result:" + outputSb.toString());
            return true;
        }
    }

    // 改用这个方法
    public static boolean connectionTest(String urlAddress) {
    
    
        String logPrefix = "connectionTest " + urlAddress;
        try {
    
    
            URL url = new URL(urlAddress);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            try {
    
    
                connection.setRequestMethod("HEAD");
                connection.setConnectTimeout(500);
                connection.setReadTimeout(500);
                int code = connection.getResponseCode();
                Log.d(TAG, logPrefix + " success! " + code);
                return true;
            } finally {
    
    
                connection.disconnect();
            }
        } catch (MalformedURLException e) {
    
    
            Log.w(TAG, "urlAddress " + urlAddress + " is a malformed URL!");
            return false;
        } catch (UnknownHostException e) {
    
    
            Log.w(TAG, logPrefix + " UnknownHostException");
            return false;
        } catch (IOException e) {
    
    
            Log.w(TAG, logPrefix + " exception", e);
            return false;
        }
    }

猜你喜欢

转载自blog.csdn.net/hegan2010/article/details/105249264