【问题标题】:Find IP Address of android device running web service查找运行 Web 服务的 android 设备的 IP 地址
【发布时间】:2012-03-31 14:54:10
【问题描述】:

我已经在我的安卓设备中设置了一个网络服务。现在我想通过WiFi从PC向android发送请求。我需要我的 android 设备的 ip 地址才能从同一网络中的 pc 访问它。如何通过我的代码找到 IP?

谁能帮帮我?

提前谢谢..

【问题讨论】:

标签: java android web-services network-protocols


【解决方案1】:

获取设备ip地址使用此方法:

public String getLocalIpAddress() {
    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) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}

如果此方法返回 null,则没有可用的连接。 如果方法返回字符串,则该字符串包含设备当前使用的ip地址,与3G或WiFi无关。

【讨论】:

    【解决方案2】:

    刚刚找到如何获取您的内部 IP: 设置 >> 无线控制 >> Wi-Fi 设置

    在底部的“Wi-Fi 网络”下 点按您连接的连接

    它会弹出一个窗口,其中包含如下信息:

    Status
    Speed
    Signal Strength
    Security
    **IP Address**
    

    【讨论】:

    • 正确,但在 DHCP 配置中,这永远不能保证保持不变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2011-07-02
    • 2014-03-15
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多