android设备插网线的时候 , 如何获取以太网IP ?
1条回答 默认 最新
- nanl1989 2017-04-14 10:32关注
public static String getHostIP() {
String hostIp = null; try { Enumeration nis = NetworkInterface.getNetworkInterfaces(); InetAddress ia = null; while (nis.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) nis.nextElement(); Enumeration<InetAddress> ias = ni.getInetAddresses(); while (ias.hasMoreElements()) { ia = ias.nextElement(); if (ia instanceof Inet6Address) { continue;// skip ipv6 } String ip = ia.getHostAddress(); if (!"127.0.0.1".equals(ip)) { hostIp = ia.getHostAddress(); break; } } } } catch (SocketException e) { Log.i("yao", "SocketException"); e.printStackTrace(); } return hostIp; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 4