【问题标题】:Getting the IP address of the android device when connected to 3G mobile network安卓设备连接3G移动网络时获取IP地址
【发布时间】:2011-08-14 16:38:25
【问题描述】:

当我连接WiFi时,我可以获取Android手机的IP地址。

但是,在3G等移动网络上,是否还能获取安卓手机的IP地址?
如果是,请发布相同的代码。

【问题讨论】:

    标签: java android ip-address 3g-network


    【解决方案1】:

    试试这样的

    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) {}
    

    【讨论】:

    • 获取 ip 地址 0.0.0.0
    • @Josnidhin 因为它在循环中运行,您如何确保您实际上获得了正确的 ip?使用这种方法获取的最后一个 ip(在循环迭代期间找到)可能会覆盖真正的 ip。
    【解决方案2】:

    移动设备通过 3G 连接浏览时没有 ip,您将在服务器端代码上获取 ISP ip。如果可能,我建议您将 ip 替换为唯一 id、设备类型和坐标。

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      相关资源
      最近更新 更多