【发布时间】:2012-05-14 10:17:34
【问题描述】:
我试图找到我的网络的 ip 并使用该 ip 获取位置。但我的代码在连接到 2G 网络时返回正确的 ip(223.187.19.157)。但是当我连接到我的 wifi 时,它会像这样返回 ip fe80::7ad6:f0ff:fe2b:dca6%wlan0 。谁能帮我找到正确格式的wifi ip。代码如下
public String getIpAddress() {
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) {
ex.printStackTrace();
}
return null;
}
【问题讨论】:
-
您需要按照in this SO-answer的描述使用WifiManager
-
我的中文平板电脑上的这个 sn-p 有完全相同的问题。但它在我的 SGS 手机上运行正常。
标签: android