Android执行curl命令获取dns解析时间

参考链接:
https://www.cnblogs.com/weiyiming007/p/10675053.html
https://www.jianshu.com/p/40f698157330

获取dns解析时间

和linux中一样,使用curl命令可以获取dns解析时间,命令如下

curl -o /dev/null -s -w %{time_namelookup}  http://www.baidu.com

Android中执行linux命令,代码如下:

String getdnstime(){
        Runtime mRuntime = Runtime.getRuntime();
        try {
            //Process中封装了返回的结果和执行错误的结果
            Process mProcess = mRuntime.exec("curl -o /dev/null -s -w %{time_namelookup}  http://www.baidu.com");
            BufferedReader mReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
            StringBuffer mRespBuff = new StringBuffer();
            char[] buff = new char[1024];
            int ch = 0;
            while ((ch = mReader.read(buff)) != -1) {
                mRespBuff.append(buff, 0, ch);
            }
            mReader.close();
            return mRespBuff.toString();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

你可能感兴趣的:(Android执行curl命令获取dns解析时间)