Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我怎样才能得到平的时间?_Android_Ping - Fatal编程技术网

Android 我怎样才能得到平的时间?

Android 我怎样才能得到平的时间?,android,ping,Android,Ping,我使用此代码ping网站google.com public String ping(String url) { String str = ""; try { Process process = Runtime.getRuntime().exec( "/system/bin/ping -c 1 " + url); BufferedReader reader = new Buffer

我使用此代码ping网站google.com

    public String ping(String url) {
       String str = "";
       try {
           Process process = Runtime.getRuntime().exec(
                   "/system/bin/ping -c 1 " + url);
           BufferedReader reader = new BufferedReader(new InputStreamReader(
                   process.getInputStream()));

           int i;
           char[] buffer = new char[4096];
           StringBuffer output = new StringBuffer();

           while ((i = reader.read(buffer)) > 0)
               output.append(buffer, 0, i);
           reader.close();

           // body.append(output.toString()+"\n");
           str = output.toString();
           Log.d("str", str);
       }
       catch (IOException e) {
           // body.append("Error\n");
           e.printStackTrace();
       }
       return str;
   }
日志cat输出如下所示:

11-12 07:23:34.028: D/str(1399): PING www.google.com (216.58.209.196) 56(84) bytes of data. 
11-12 07:23:34.028: D/str(1399): 64 bytes from bud02s22-in-f4.1e100.net (216.58.209.196): icmp_seq=1 ttl=48
**time=149 ms** 
11-12 07:23:34.028: D/str(1399): --- www.google.com ping statistics --- 
11-12 07:23:34.028: D/str(1399): 1 packets transmitted, 1 received, 0% packet loss, time 0ms 
11-12 07:23:34.028: D/str(1399): rtt min/avg/max/mdev = 149.750/149.750/149.750/0.000 ms
有人知道我怎样才能得到“时间=149毫秒”的值吗? 提前谢谢

使用方法查找str变量中的所有“time=”子字符串,并获取每个“time=”子字符串后面的数字。 或者使用ReqularExpression

您可以使用正则表达式:

/.*time=([0-9]+)\s(ms).*/
您需要的信息位于两个捕获组中。我推荐regex的原因是,如果您需要其他信息,您可以轻松地扩展它

sed
上述示例(
d
包含您的字符串):

以下是如何在java中使用上述方法:。

在您的案例中,请尝试以下方法:

    try {
            Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 " + url);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            int i;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();

            while ((i = reader.read(buffer)) > 0)
                output.append(buffer, 0, i);
            reader.close();

            // body.append(output.toString()+"\n");
            str = output.toString();

            if (str.contentEquals("time=")) {
                str = str.substring(str.indexOf("time="), str.length());
                Log.d("str", str);
            }
        }

你有一根绳子。这里有很多问题(和答案)如何从字符串中提取数字。谢谢@GeraldSchneider我会检查i,时间可能是浮动的,所以我使用“(time=)(.*)(ms)”作为正则表达式。
    try {
            Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 " + url);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            int i;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();

            while ((i = reader.read(buffer)) > 0)
                output.append(buffer, 0, i);
            reader.close();

            // body.append(output.toString()+"\n");
            str = output.toString();

            if (str.contentEquals("time=")) {
                str = str.substring(str.indexOf("time="), str.length());
                Log.d("str", str);
            }
        }