切换到 Android Marshmallow API,我在代码中使用org.apache.http.conn.util.InetAddressUtils
forInetAddressUtils.isIPv4Address(ipAddress)
来列出设备中的所有 IP。
作为API-23 更改的一部分,InetAddressUtils
该类现已消失。
我现在如何替换下面的代码?
public static String ipAddress() {
try {
for (final Enumeration<NetworkInterface> enumerationNetworkInterface = NetworkInterface.getNetworkInterfaces(); enumerationNetworkInterface.hasMoreElements();) {
final NetworkInterface networkInterface = enumerationNetworkInterface.nextElement();
for (Enumeration<InetAddress> enumerationInetAddress = networkInterface.getInetAddresses(); enumerationInetAddress.hasMoreElements();) {
final InetAddress inetAddress = enumerationInetAddress.nextElement();
final String ipAddress = inetAddress.getHostAddress();
if (! inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipAddress)) {
return ipAddress;
}
}
}
return null;
}
catch (final Exception e) {
LogHelper.wtf(null, e);
return null;
}
}