我已经能够在android中执行基本ping,并且能够在android中使用Processes获得结果。我的代码如下
try {
String pingCmd = "ping -s 100 -c 10 " + host;
String pingResult = "";
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);
BufferedReader in = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
text.setText(inputLine + "\n\n");
pingResult += inputLine;
text.setText(pingResult);
}
in.close();
}//try
catch (IOException e) {
System.out.println(e);
}
我正在运行以下命令String pingCmd = "ping -s 100 -c 10 " + host;
。我想获得每个ping的unix时间戳我已经尝试过以下内容并且没有得到任何结果:
String pingCmd = "ping -s 100 -c 10 " + host+ " -D";
String pingCmd = "ping -s 100 -c 10 " + host+ "| while read pong; do echo "$(date): $pong"; done";
String pingCmd = "ping -s 100 -c 10 " + host+ "| perl -nle 'print scalar(localtime), " ", $_'";
String pingCmd = "ping -s 100 -c 10 " + host+ "| xargs -L 1 -I '{}' date '+%+: {}'";
当我在textview中执行这些操作时,我没有收到任何错误。请告诉我如何获取每个ping的unix时间戳
我希望我的输出看起来像每个ping
[1461167279.090372] 108 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_seq=1 ttl=51 time=335 ms
答案 0 :(得分:0)
您应该查看错误输出流(p.getErrorStream()
),我认为android下的ping不会处理所有切换,并在该流上打印错误。