java 使用java获取Android项目中计算机的IP地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5307992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:32:36  来源:igfitidea点击:

Get the IPaddress of the computer in an Android project using java

javaandroidip-addressipandroid-ksoap2

提问by shadesco

I am using ksoap2-androidand i need to get the IP address using java so that I don't have to type it manually everytime.

我正在使用ksoap2-android,我需要使用 java 获取 IP 地址,这样我就不必每次都手动输入它。

What i mean by IP address is , for example if I do ipconfigusing the command shell:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : f0::ed2:e3bf:8206:44%13
IPv4 Address. . . . . . . . . . . : 192.168.1.107 <--THIS ONE
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1

我所说的 IP 地址是什么意思,例如,如果我使用命令 shell执行ipconfig
Connection-specific DNS Suffix 。:
链路本地 IPv6 地址。. . . . : f0::ed2:e3bf:8206:44%13
IPv4 地址。. . . . . . . . . . : 192.168.1.107 <--这个
子网掩码。. . . . . . . . . . :255.255.255.0
默认网关。. . . . . . . . : 192.168.1.1

The thing is am developing an android app, and the emulator has a different type of IP's than the machine's .
I need to get the machine's IP , how is this to be done?

事情是正在开发一个 android 应用程序,并且模拟器的 IP 类型与机器的 .
我需要获取机器的IP,这要怎么做?

thanks a lot

多谢

回答by Hades

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(tag, ex.toString());
        }
        return "";
    }

回答by JoshuaBlr

To get the Ipaddress of your android device you use this code.

要获取您的 android 设备的 Ipaddress,您可以使用此代码。

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = intToIp(ipAddress);

public String intToIp(int i) {

   return ((i >> 24 ) & 0xFF ) + "." +
               ((i >> 16 ) & 0xFF) + "." +
               ((i >> 8 ) & 0xFF) + "." +
               ( i & 0xFF) ;
}

回答by Rohit Mandiwal

Try this link

试试这个链接

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

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

also you can try this command adb shell netcfg

你也可以试试这个命令adb shell netcfg

回答by Jigar Joshi

InetAddress iA=InetAddress.getLocalHost();
System.out.println(iA.getHostAddress());


See Also

也可以看看