android中ip和hosts地址的获取

来源:互联网 发布:产品网络营销策划书 编辑:程序博客网 时间:2024/05/08 07:55
public static String getMyIP() {        try {            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {                NetworkInterface intf = en.nextElement();                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {                    InetAddress inetAddress = enumIpAddr.nextElement();                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {                        return inetAddress.getHostAddress().toString();                    }                }            }        } catch (Exception e) {        }        return "";    }    public static String getDNSIP(String host) {        InetAddress x;        try {            x = InetAddress.getByName(host);            String ip = x.getHostAddress();//得到字符串形式的ip地址            return ip;        } catch (UnknownHostException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return "";    }
暂时没有看源码,有时间补上
0 0
原创粉丝点击