Android Ping网址、IP以及网关地址方法汇总
//判断传入的url地址是否为ip地址
public boolean isIP(String string){
if(string.toLowerCase().contains("http://") || string.toLowerCase().contains("https://")){
string = string.split("/")[2];
}else if(string.startsWith("/")){
string = string.split("/")[1];
}else{
string = string.split("/")[0];
}
if(string.contains(":")){
string = string.split(":")[0];
}
if(string.length() < 7 || string.length() > 15 || "".equals(string))
{
return false;
}
/**
* 判断IP格式和范围
*/
String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
Pattern pat = Pattern.compile(rexp);
Matcher mat = pat.matcher(string);
boolean ipAddress = mat.find();
return ipAddress;
}
//获取传入url地址的域名
public boolean obtainDomain(String string){
String domain = "";
if(string.toLowerCase().contains("http://") || string.toLowerCase().contains("https://")){
domain = string.split("/")[2];
}else if(string.startsWith("/")){
domain = string.split("/")[1];
}else{
domain = string.split("/")[0];
}
if(domain.contains(":")){
domain = domain.split(":")[0];
}
return domain;
}
//获取传入url的ip地址
private String obtainIP(String url){
java.security.Security.setProperty("networkaddress.cache.ttl", "0");
return InetAddress.getByName(url).getHostAddress();
}
//获取网关地址
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo info=wifiManager..getDhcpInfo();
int gateway=info.gateway;
String ip=intToIp(gateway);
/**
* int值转换为ip
* @param addr
* @return
*/
public static String intToIp(int addr) {
return ((addr & 0xFF) + "." +
((addr >>>= 8) & 0xFF) + "." +
((addr >>>= 8) & 0xFF) + "." +
((addr >>>= 8) & 0xFF));
}
String cmd = "ping -c " + pingCount + " -i " + pingInterval + " -w " + timeout + " -s "
+ packageSize + " -t " + ttlSize+ " -W " + 1000 + " " + ipUrl;
private static String getPingResult(String cmd) {
StringBuilder sb = new StringBuilder();
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);
BufferedReader read = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = "";
String send = "";
String receive = "";
String lost = "";
while ((line = read.readLine()) != null) {
Log.i("ping===>", "====readLine====>" + line, uuid);
if (line.contains("bytes from") && line.contains("ttl=") && line.contains("time=")) {
String time = "--";
try {
time = line.split("time=")[1].trim().split(" ms")[0].trim();
} catch (NumberFormatException e) {
}
}else if (line.contains("packets transmitted" ) && line.contains("received")){
//2013-06-18 14:08:18: 4 packets transmitted, 4 received, 0% packet loss, time 3005ms ping数据类型,按需截取
String[] temp = line.split(":");
String res = temp[temp.length-1]; //分开时间与返回的结果
String[] resArray = res.split(","); //分开每个结果
String time = "";
for(String s : resArray){
if(s.contains("packets transmitted")){
send = s.split("packets transmitted")[0].trim();
}else if (s.contains("received")){
receive = s.split("received")[0].trim();
}else if (s.contains("packet loss")){
lost = s.split("packet loss")[0].trim();
}else if (s.contains("time")){
time = s.split("time")[1].trim();
}
}
String successRate = new DecimalFormat("0").format(Double.parseDouble(send) == 0 ?
0 : (Double.parseDouble(receive) /Double.parseDouble(send) * 100)) +"%";
}else if (line.contains("rtt min/avg/max/mdev =")){
//2013-06-18 14:31:12: rtt min/avg/max/mdev = 138.426/140.371/141.689/1.291 ms ping数据类型,按需截取
String res = line.split("=")[1].split("ms")[0].trim();
String[] temp = res.split("\\/");
String avg = temp[1];
break;
}else{
//ignore it
}
}
process.waitFor();
}catch (IOException e) {
e.printStackTrace();
}catch(InterruptedException e) {
e.printStackTrace();
}finally{
if(process != null){
process.destroy();
process = null;
}
}
return sb.toString();
}