作者ohohyeah (欧欧耶)
看板AndroidDev
标题[问题] 如何取得平板电脑的IP (IPv4)
时间Sun Oct 2 23:54:58 2011
如提
我想抓AP派下来给手机和平板电脑的IP
於是在网路上找到了一个范例
在手机上OK
但是在平板电脑却return一个 IPv6的IP
造成极大的困扰Q___Q
请问各位怎样才能能取得IPv4的IP 谢谢!!
code如下
public static String getLocalIpAddress() {//抓AP给的IP
try {
for(Enumeration<NetworkInterface> en =
NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
NetworkInterface intf = en.nextElement();
for(Enumeration<InetAddress> enumIpAddr =
intf.getInetAddresses(); enumIpAddr.hasMoreElements();){
InetAddress inetAddress = enumIpAddr.nextElement();
if(!inetAddress.isLoopbackAddress()){
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("SocketException", ex.toString());
}
return null;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.165.56.179
1F:→ sjrom:多个判断 && inetAddress instanceof Inet4Address 10/03 07:15
2F:→ ohohyeah:感谢您!!! 10/11 00:25