Android API-23: InetAddressUtils replacement
Asked Answered
W

5

19

Switching to Android Marshmallow API, I was using org.apache.http.conn.util.InetAddressUtils for InetAddressUtils.isIPv4Address(ipAddress) in a code to list all IPs from a device.

As part of the API-23 changes, the InetAddressUtils class is now gone.

How can I replace the below code now?

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;
    }
}
Welloff answered 21/8, 2015 at 13:38 Comment(3)
What are you trying to do? If you just walt to know if an IP is IPv6 or not you can check for inetAddress instancof Inet6Address or inetAddress instancof Inet4Address.Nesbitt
I'm trying to replace InetAddressUtils.isIPv4Address(ipAddress) with a code that works with Android API-23Welloff
Like its names says: returns if the ip address is IPv4 or not hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/… This method is now unavailable in Android API-23. I just want to replace it but I don't know any alternatives yet.Welloff
N
26

Like I interprete from the comments you can replace that function with this comparison:

inetAddress instanceof Inet4Address

so your code would end in:

if(!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {

Update from 2020
Keep in mind that your user can also have IPv6 enabled. In that case you need to check for Inet6Address too.

Nesbitt answered 24/8, 2015 at 9:2 Comment(3)
Well, indeed InetAddressUtils is gone from Android's API-23, but not Inet4Address. Thanks for the workaround.Welloff
Or you can also do: Inet4Address.class.isInstance(inetAddress)Mallina
@maňish: import java.net.Inet4AddressPremillennial
D
5

Add below to your build.gradle(Module:app) file,

android { useLibrary 'org.apache.http.legacy' }

Digression answered 16/3, 2018 at 6:22 Comment(0)
H
4

I couldn't find something better than converting to Inet4Address or 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;
     } 
}

Note, the getHostByName actually does the lookup, which isn't always desirable.

Or, you can get source of InetAddessUtils, which unlike getByName(), doesn't do the lookup, but accepts only dotted addresses. The code is really tiny. It uses regexp classes which are supported by Android. Just remove Immutable annotation which isn't really important, and it will compile!

Haemocyte answered 30/8, 2015 at 0:9 Comment(1)
getByName() seems to me like inherited from InetAddress.Welloff
E
3

To use this library in SDK 23 add following line in project's build.gradle file:

useLibrary 'org.apache.http.legacy'
Eldoneldora answered 29/9, 2016 at 5:32 Comment(1)
That would also make the legacy code work. However I was looking for a replacement and not having to depend on an entire legacy for this function. Other might be looking for this solution though, upvoted.Welloff
E
1

Using try catch as logic is horrible practice and should only be done if totally unavoidable..

Use something like this instead:

if (inetAddress instanceof Inet4Address){
    //do something
}  
Errecart answered 27/4, 2016 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.