我做了一个android应用程序,它从位于web服务器上的数据库(php,json,…)中读取。如果服务器关闭或找不到ip地址,应用程序显示:
很遗憾,该应用程序已被停止。
我想显示自定义的错误消息。示例"无法访问服务器",应用程序继续正常工作。
如何检查服务器或ip地址是否可访问或可用以避免预览消息?
此代码解决的问题:
public static boolean ping()
{
System.out.println(" executeCammand");
Runtime runtime = Runtime.getRuntime();
try
{
Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 "+server);
int mExitValue = mIpAddrProcess.waitFor();
if(mExitValue==0){
return true;
}else{
return false;
}
}
catch (InterruptedException ignore)
{
}
catch (IOException e)
{
}
return false;
}