Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android API-23: InetAddressUtils replacement

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;
    }
}
like image 515
shkschneider Avatar asked Aug 21 '15 13:08

shkschneider


5 Answers

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.

like image 151
rekire Avatar answered Oct 18 '22 23:10

rekire


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

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

like image 24
dgamer Avatar answered Oct 18 '22 23:10

dgamer


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!

like image 35
cyanide Avatar answered Oct 19 '22 00:10

cyanide


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

useLibrary 'org.apache.http.legacy'
like image 3
Tarun Avatar answered Oct 18 '22 23:10

Tarun


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
}  
like image 1
Daniel Harvey Avatar answered Oct 18 '22 23:10

Daniel Harvey