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 "";
}
暂时没有看源码,有时间补上
android中ip和hosts地址的获取
最新推荐文章于 2022-10-19 10:51:46 发布