Android Ping相关方法整理

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();
	 }

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值