public String getLocalIpAddress() {
String ipaddress="";
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()) {
ipaddress=ipaddress+";"+ inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("WifiPreference IpAddress", ex.toString());
}
return ipaddress;
}转载于:https://www.cnblogs.com/nafio/p/9137654.html
本文介绍了一种使用Java代码获取设备上所有非回环本地IP地址的方法。通过遍历网络接口并检查InetAddress,该方法能够收集并返回一个包含所有有效IP地址的字符串。此代码适用于需要在网络环境中识别自身IP地址的Java应用程序。
8704

被折叠的 条评论
为什么被折叠?



