shk*_*der 19 android android-networking android-6.0-marshmallow
切换到Android棉花糖API,我使用org.apache.http.conn.util.InetAddressUtils
了InetAddressUtils.isIPv4Address(ipAddress)
一个代码,列出从设备的所有IP.
作为API-23更改的一部分,该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;
}
}
Run Code Online (Sandbox Code Playgroud)
rek*_*ire 23
就像我从评论中解释一样,您可以使用此比较替换该函数:
inetAddress instanceof Inet4Address
Run Code Online (Sandbox Code Playgroud)
所以你的代码将以:
if(!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11730 次 |
最近记录: |