如何获取我的IP地址?

sup*_*iox 8 sockets android ip-address

我有一个serverSocket,我想知道IP地址,但有

listenSocket.getInetAddress().toString();
Run Code Online (Sandbox Code Playgroud)

我得到0.0.0.0.如何获取IP地址,或者(如果启用了两个连接)其中一个?

bre*_*dan 15

我以前用过这个:

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();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

资料来源:http: //www.droidnova.com/get-the-ip-address-of-your-device,304.html