public int pingIP() {
int sumValues = 0;
try {
if(pbuilder == null){
pbuilder = new ProcessBuilder();
pbuilder.command(PING_COMMAND,mPingSepcifyIp);
pbuilder.redirectErrorStream(true);
process = pbuilder.start();
}
in = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str = null;
int times = 0;
while ((str = br.readLine()) != null) {
if (times > ALLOW_PING_TIMES) {
DebugLog.d(TAG, "process break while");
break;
}
Log.i(TAG, str+"\n");
int indexBegin = str.lastIndexOf(PING_FIND_KEY_BEGIN);
int indexEnd = str.lastIndexOf(PING_FIND_KEY_END);
Log.i(TAG, "indexBegin="+indexBegin+",indexEnd="+indexEnd+",length="+str.length());
if(indexBegin > 0 && indexEnd >0){
String newStr = (str.substring(indexBegin+1, indexEnd)).trim();
Log.e(TAG, "new string = "+newStr);
if(newStr != null || !"".equals(newStr)){
if(newStr.contains(".")){//avoid pointer
String strBuf[] = newStr.split("\\.");
DebugLog.d(TAG, "StrBuf length="+strBuf.length);
newStr = strBuf[0];
}
int cNumber = 0;
try{
cNumber = Integer.parseInt(newStr.trim());
}catch(Exception e){
e.printStackTrace();
}
DebugLog.d(TAG, "cNumber = "+cNumber);
sumValues += cNumber;
Log.e(TAG, "sumValues = "+sumValues);
}
}
times++;
}
DebugLog.i(TAG, "process end");
} catch (IOException e) {
Log.e(TAG, "thread error");
e.printStackTrace();
} finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
if(process != null){
DebugLog.d(TAG, "finally process != null");
process.destroy();
}
}
return sumValues / ALLOW_PING_TIMES;
}
int sumValues = 0;
try {
if(pbuilder == null){
pbuilder = new ProcessBuilder();
pbuilder.command(PING_COMMAND,mPingSepcifyIp);
pbuilder.redirectErrorStream(true);
process = pbuilder.start();
}
in = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str = null;
int times = 0;
while ((str = br.readLine()) != null) {
if (times > ALLOW_PING_TIMES) {
DebugLog.d(TAG, "process break while");
break;
}
Log.i(TAG, str+"\n");
int indexBegin = str.lastIndexOf(PING_FIND_KEY_BEGIN);
int indexEnd = str.lastIndexOf(PING_FIND_KEY_END);
Log.i(TAG, "indexBegin="+indexBegin+",indexEnd="+indexEnd+",length="+str.length());
if(indexBegin > 0 && indexEnd >0){
String newStr = (str.substring(indexBegin+1, indexEnd)).trim();
Log.e(TAG, "new string = "+newStr);
if(newStr != null || !"".equals(newStr)){
if(newStr.contains(".")){//avoid pointer
String strBuf[] = newStr.split("\\.");
DebugLog.d(TAG, "StrBuf length="+strBuf.length);
newStr = strBuf[0];
}
int cNumber = 0;
try{
cNumber = Integer.parseInt(newStr.trim());
}catch(Exception e){
e.printStackTrace();
}
DebugLog.d(TAG, "cNumber = "+cNumber);
sumValues += cNumber;
Log.e(TAG, "sumValues = "+sumValues);
}
}
times++;
}
DebugLog.i(TAG, "process end");
} catch (IOException e) {
Log.e(TAG, "thread error");
e.printStackTrace();
} finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
if(process != null){
DebugLog.d(TAG, "finally process != null");
process.destroy();
}
}
return sumValues / ALLOW_PING_TIMES;
}