我正在使用以下代码调用android设备ping命令:
public static String pingServer() {
PingResult result = new PingResult();
String jsonString = null;
try {
String command = "ping -c 3 192.168.8.185";
Process p = Runtime.getRuntime().exec(command);
int status = p.waitFor();
InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
buffer.append("\n");
}
String bufferStr = buffer.toString();
System.out.println(bufferStr);
} catch (Exception e) {
System.out.println("---------------exception-----------ping");
System.out.println(e.getMessage());
// e.printStackTrace();
}
}
BufferStr 总是获取最后一行数据。
3个发送,3个接收,0%丢包,时间2000ms
可以得到详细的结果。