Android 平板电脑 IP 地址

发布于 2024-12-14 03:57:09 字数 944 浏览 9 评论 0原文

在这里,我遇到了有关获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千笙结 2024-12-21 03:57:09

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.

江心雾 2024-12-21 03:57:09

那是一个 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.

泛滥成性 2024-12-21 03:57:09

/* 查看可用的网络接口并选择第一个“合适的”IPv4 地址。
* 由于模拟器默认使用 10.0.2.15,因此仅在没有更好的可用版本时才使用它。 */

public String getMyIp() {
        Set<String> eligible = eligibleIpAddresses();

/* For the emulator, prefer an IP address other than 10.0.2.15 (default emulator address)
             * but use it if it is the only one. */
        if (eligible.size() > 1) {
            eligible.remove("10.0.2.15");
            return eligible.iterator().next();
        } else if (eligible.size() == 1) {
            return eligible.iterator().next();
        } else {
            Log.w("Using local IP address, no external objects will be discovered","---");
            return "127.0.0.1";
        }
    }

    public static Set<String> eligibleIpAddresses() {
        Set<String> eligible = new HashSet<String>();
        try {
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                Enumeration<InetAddress> address = ni.getInetAddresses();
                while (address.hasMoreElements()) {
                    InetAddress addr = address.nextElement();
                    if (!addr.isLoopbackAddress() && !(addr.getHostAddress().indexOf(":") > -1)) {
                        eligible.add(addr.getHostAddress());
                    }
                }
            }
        } catch (Exception e) {
        }
        return eligible;
    }

/* 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. */

public String getMyIp() {
        Set<String> eligible = eligibleIpAddresses();

/* For the emulator, prefer an IP address other than 10.0.2.15 (default emulator address)
             * but use it if it is the only one. */
        if (eligible.size() > 1) {
            eligible.remove("10.0.2.15");
            return eligible.iterator().next();
        } else if (eligible.size() == 1) {
            return eligible.iterator().next();
        } else {
            Log.w("Using local IP address, no external objects will be discovered","---");
            return "127.0.0.1";
        }
    }

    public static Set<String> eligibleIpAddresses() {
        Set<String> eligible = new HashSet<String>();
        try {
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                Enumeration<InetAddress> address = ni.getInetAddresses();
                while (address.hasMoreElements()) {
                    InetAddress addr = address.nextElement();
                    if (!addr.isLoopbackAddress() && !(addr.getHostAddress().indexOf(":") > -1)) {
                        eligible.add(addr.getHostAddress());
                    }
                }
            }
        } catch (Exception e) {
        }
        return eligible;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文