【问题标题】:Android device returns invalid IP addressAndroid 设备返回无效 IP 地址
【发布时间】:2013-07-18 11:19:13
【问题描述】:

您好,我正在使用下面的代码获取android设备的IP地址,

private String returnIPAdrress()
        {
            String IPAddress = null;
            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())
                                        {
                                            IPAddress = inetAddress.getHostAddress().toString();
                                        }
                                }
                        }

                }
            catch (SocketException ex)
                {
                    Log.e("ServerActivity", ex.toString());
                    return null;
                }
            return IPAddress;
        }

当我在 Galaxy 平板电脑 (os=2.3) 上对其进行测试时,它运行良好,并为我提供了有效的 IP 地址。

我已经在模拟器(os=2.2)上对其进行了测试,它给我的 IP 地址为 10.0.2.15,我猜这也是有效的。

但是当在 Micromax canvas(os=4.1) 上运行它时,它给我的 IP 地址为 fe80::d0b3:3fff:fe9d:f68c%p2p0 这是错误的.

是因为操作系统版本不同吗?

我该如何解决这个问题?

【问题讨论】:

标签: android networking ip


【解决方案1】:

试试这个方法:

public static String getIPAddress() {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (isIPv4 && intf.getDisplayName().startsWith("wlan")) {
                        return sAddr;
                    }
                }
            }
        }
    } catch (Exception ex) {
        return null;
    }
    return null;
}

【讨论】:

  • +1 用于检查其ipv4 地址,但如果果冻豆支持ipv6,那么我也需要处理它。
  • 此方法适用于 jellybean,它同时具有 ipv4 和 ipv6 地址!如果需要ipv6地址,去掉那个就是IPv4检查……
  • 但是ipv4和ipv6之间没有1-1的关系;这意味着我们可以将所有的ipv4地址转换成ipv6但不能反过来..
  • 但这与原始问题没有任何关系...您将 ipv6 地址称为“错误”...
  • 我很抱歉,当我发布问题时我不知道 jellybean 使用 ipv6
【解决方案2】:

您可以使用dhcp.ipaddress访问设备的IP地址

private final WifiManager manager;
private final DhcpInfo dhcp;     
private InetAddress getMyIP() {
    manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    dhcp = manager.getDhcpInfo();
    final String address = Formatter.formatIpAddress(dhcp.ipAddress); // ipAddress - IP address of my device, assigned through dhcp
    InetAddress myIP = null;

    try {
        myIP = InetAddress.getByName(address);

        Log.i("My IP  "," + myIP.toString());
    } catch (Exception e) {
        Log.e("Cannot find my own IP. Error ", e.toString());
    }

    return myIP;
}

【讨论】:

    猜你喜欢
    • 2013-05-30
    • 2019-11-13
    • 2013-04-25
    • 2022-11-22
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    相关资源
    最近更新 更多