本文介绍了Android API-23:InetAddressUtils替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
切换到Android Marshmallow API时,我在代码中将org.apache.http.conn.util.InetAddressUtils
用作InetAddressUtils.isIPv4Address(ipAddress)
,以列出设备中的所有IP.
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.
作为 API-23更改的一部分,InetAddressUtils
类现在消失了
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 I interprete from the comments you can replace that function with this comparison:
inetAddress instanceof Inet4Address
因此您的代码将以:
if(!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
这篇关于Android API-23:InetAddressUtils替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!