public static String getLocalIpAddress() {
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()
&& InetAddressUtils.isIPv4Address(inetAddress
.getHostAddress())) {
System.out.println("============"
+ inetAddress.getHostAddress().toString());
} else {
Log.e("WifiPreference IpAddress---error-", "ddddd");
}
}
}
return "错误";
} catch (SocketException e) {
e.printStackTrace();
}
return null;
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()
&& InetAddressUtils.isIPv4Address(inetAddress
.getHostAddress())) {
System.out.println("============"
+ inetAddress.getHostAddress().toString());
} else {
Log.e("WifiPreference IpAddress---error-", "ddddd");
}
}
}
return "错误";
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
记得加上网络权限