我正在尝试获取设备的IP地址,即使用WIFI或3G连接。我得到IPV6格式的IP地址,这是不可理解的。我想IPV4格式的IP地址。我已经完成谷歌,但力德发现任何适当的解决方案。如何获得IPV4格式的IP_ADDRESS
这里是我使用得到一个设备的IP地址码
public String getLocalIpAddress() {
try {
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();
System.out.println("ip1--:" + inetAddress);
System.out.println("ip2--:" + inetAddress.getHostAddress());
if (!inetAddress.isLoopbackAddress()) {
String ip = inetAddress.getHostAddress().toString();
System.out.println("ip---::" + ip);
EditText tv = (EditText) findViewById(R.id.ipadd);
tv.setText(ip);
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception ex) {
Log.e("IP Address", ex.toString());
}
return null;
}
我得到这个输出中:
ip1--:/fe80::5054:ff:fe12:3456%eth0%2
ip2--:fe80::5054:ff:fe12:3456%eth0
应该显示如下:
192.168.1.1
请帮我解决..
不要忘记权限,以防止空结果: –
Vyacheslav
声明“ipv4”的地方和声明 – CrazyMind