Android API-23:InetAddressUtils替换

Android API-23:InetAddressUtils替换,android,android-networking,android-6.0-marshmallow,Android,Android Networking,Android 6.0 Marshmallow,切换到Android棉花糖API,我在一个代码中使用org.apache.http.conn.util.InetAddressUtilsforInetAddressUtils.isIPv4Address(ipAddress)列出设备中的所有IP 作为的一部分,InetAddressUtils类现在不存在了 我现在如何替换下面的代码 public static String ipAddress() { try { for (final Enumeration<Netw

切换到Android棉花糖API,我在一个代码中使用
org.apache.http.conn.util.InetAddressUtils
for
InetAddressUtils.isIPv4Address(ipAddress)
列出设备中的所有IP

作为的一部分,
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;
    }
}
公共静态字符串ipAddress(){
试一试{
对于(最终枚举enumerationNetworkInterface=NetworkInterface.getNetworkInterfaces();enumerationNetworkInterface.hasMoreElements();){
final NetworkInterface NetworkInterface=enumerationNetworkInterface.nextElement();
对于(枚举EnumerationItemAddress=networkInterface.getInetAddresses();枚举EnumerationItemAddress.hasMoreElements();){
final InetAddress InetAddress=枚举地址.nextElement();
最后一个字符串ipAddress=inetAddress.getHostAddress();
如果(!inetAddress.isLoopbackAddress()&&InetAddressUtils.isIPv4Address(ipAddress)){
返回IP地址;
}
}
}
返回null;
}
捕获(最终异常e){
LogHelper.wtf(null,e);
返回null;
}
}

就像我从注释中解释的那样,您可以使用此比较替换该函数:

inetAddress instanceof Inet4Address
因此,您的代码将以:

if(!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
从2020年起更新
请记住,您的用户也可以启用IPv6。在这种情况下,您还需要检查
Inet6Address

我找不到比转换为Inet4Address或Inet6Address更好的方法

public boolean isValidIp4Address(final String hostName) {
      try {
         return Inet4Address.getByName(hostName) != null;
     } catch (UnknownHostException ex) {
         return false;
     } 
}

public boolean isValidIp6Address(final String hostName) {
      try {
         return Inet6Address.getByName(hostName) != null;
     } catch (UnknownHostException ex) {
         return false;
     } 
}
注意,getHostByName实际上执行查找,这并不总是可取的


或者,您可以获取的源,它与getByName()不同,不执行查找,但只接受虚线地址。代码非常小。它使用Android支持的regexp类。只需删除不可变的注释,这并不重要,它就会编译

使用try-catch作为逻辑是可怕的做法,只有在完全不可避免的情况下才应该这样做

改用类似的方式:

if (inetAddress instanceof Inet4Address){
    //do something
}  

要在SDK 23中使用此库,请在project的build.gradle文件中添加以下行:

useLibrary 'org.apache.http.legacy'

将以下内容添加到build.gradle(模块:app)文件中,

安卓{ useLibrary'org.apache.http.legacy' }


你想干什么?如果您只是想知道IP是否为IPv6,您可以检查
inetAddress instancofinet6address
inetAddress instancofinet4address
。我正在尝试替换
InetAddressUtils.isIPv4Address(ipAddress)
使用与Android API-23配合使用的代码,如其名称所示:返回ip地址是否为IPv4。此方法现在在Android API-23中不可用。我只是想替换它,但我还不知道有什么替代方案。事实上,
InetAddressUtils
已经从Android的API-23中消失了,但没有
Inet4Address
。谢谢你的解决方法。或者你也可以这样做:Inet4Address.class.isInstance(inetAddress)@maňish:import java.net.Inet4Address
getByName()
在我看来就像是继承自
inetAddress
。这也会使遗留代码正常工作。然而,我正在寻找一个替代品,而不必依赖于整个遗留的功能。不过,其他人可能正在寻找这个解决方案,但投票结果是一致的。