class ip {
public static String getIp(){
try{
for(Enumeration<NetworkInterface> enumeration=NetworkInterface.getNetworkInterfaces();enumeration.hasMoreElements();)
{
NetworkInterface intf=enumeration.nextElement();
for(Enumeration<InetAddress> ipaddress=intf.getInetAddresses();ipaddress.hasMoreElements();)
{
return inetAddress.getHostAddress();
}
//throws与throws的区别
//InetAddress.getLocalHost();这种几本上获取的是本机的回送地址(也就是系统管理地址)
}
}
}
catch(SocketException sex){
System.out.println("IP Exception");
}
return null;
}
}
public static String getIp(){
try{
for(Enumeration<NetworkInterface> enumeration=NetworkInterface.getNetworkInterfaces();enumeration.hasMoreElements();)
{
NetworkInterface intf=enumeration.nextElement();
for(Enumeration<InetAddress> ipaddress=intf.getInetAddresses();ipaddress.hasMoreElements();)
{
InetAddress inetAddress=ipaddress.nextElement();
//判断IP地址是否为IP4,安卓4.0中支持IPV6的地址
return inetAddress.getHostAddress();
}
//throws与throws的区别
//InetAddress.getLocalHost();这种几本上获取的是本机的回送地址(也就是系统管理地址)
}
}
}
catch(SocketException sex){
System.out.println("IP Exception");
}
return null;
}
}