Android 平板电脑 IP 地址
在这里,我遇到了有关获取 Android 平板电脑 IP 地址的问题。
我以通用方式使用以下代码作为平板电脑 IP 地址。
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();
}
}
}
inetAddress.getHostAddress() 方法返回 IP 地址为 fe80::9a4b:4aff:fe00:a6e1 ,这是一种与 122.xx.xxx 不同的格式.xxx 格式。
当我使用 Wifimanager 类获取平板电脑 IP 地址时,它仅以这种格式返回 122.xx.xxx.xxx 。
但使用通用方式我不知道为什么它会给出错误的格式。
任何人都可以帮助我解决这个问题...
提前致谢。
Here I am facing an issue regarding obtaining Android Tablet IP address.
I am using the following code for the tablet IP addess in a generic way.
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();
}
}
}
inetAddress.getHostAddress() method returns IP address as fe80::9a4b:4aff:fe00:a6e1 ,which is a different format rather than 122.xx.xxx.xxx
format.
When I use Wifimanager class to obtain tablet IP address it returns 122.xx.xxx.xxx in this format only.
But using the generic way I don't know why it is giving as wrong format.
Can any one please help me on this issue...
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
fe80::9a4b:4aff:fe00:a6e1
完全没有错误。它只是一种新型的 IPv6。新的应用程序始终应该设计为能够以两种格式运行。
fe80::9a4b:4aff:fe00:a6e1
is not wrong at all. It is just new-style, IPv6.New appliactions always should be designed to be able to work in both formats.
那是一个 IPv6 地址。首先查看维基百科文章。可能可以将 IPv6 地址转换为 IPv4 地址(即 xxx.xxx.xxx.xxx),但不能保证。
That is an IPv6 address. Have a look at wikipedia article for a start. It may be possible to convert an IPv6 address to an IPv4 address (i.e. xxx.xxx.xxx.xxx) but it's not guaranteed.
/* 查看可用的网络接口并选择第一个“合适的”IPv4 地址。
* 由于模拟器默认使用 10.0.2.15,因此仅在没有更好的可用版本时才使用它。 */
/* look through the available network interfaces and pick the first "decent" IPv4 address.
* As the emulator uses 10.0.2.15 by default, only use it if nothing better is available. */