编程语言
首页 > 编程语言> > java – 查找运行web服务的android设备的IP地址

java – 查找运行web服务的android设备的IP地址

作者:互联网

我在我的Android设备中设置了一个Web服务.现在我想通过WiFi从PC发送请求到android.我需要我的Android设备的IP地址从同一网络中的PC访问它.如何通过我的代码找到IP?

谁能帮我?

提前致谢..

解决方法:

要获取设备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,则没有可用的连接.
如果该方法返回一个字符串,则该字符串包含设备当前使用的独立于3G或WiFi的IP地址.

标签:android,java,web-services,network-protocols
来源: https://codeday.me/bug/20190902/1790361.html