【发布时间】:2015-11-15 11:38:31
【问题描述】:
切换到 Android Marshmallow API,我在代码中使用 org.apache.http.conn.util.InetAddressUtils 代替 InetAddressUtils.isIPv4Address(ipAddress) 来列出设备中的所有 IP。
作为API-23 changes 的一部分,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;
}
}
【问题讨论】:
-
你想做什么?如果您只是想知道 IP 是否为 IPv6,您可以检查
inetAddress instancof Inet6Address或inetAddress instancof Inet4Address。 -
我正在尝试用适用于 Android API-23 的代码替换
InetAddressUtils.isIPv4Address(ipAddress) -
正如它的名字所说:如果IP地址是IPv4或者不是IPv4返回hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/…这个方法现在在Android API-23中不可用。我只是想替换它,但我还不知道任何替代品。
标签: android android-networking android-6.0-marshmallow