Tôi đang cố gắng lấy địa chỉ IP của thiết bị, tức là sử dụng kết nối WIFI hoặc 3G. Tôi nhận được địa chỉ IP ở định dạng IPV6 mà không phải là dễ hiểu. Tôi muốn trong IPV4 định dạng địa chỉ IP. Tôi đã thực hiện google nhưng dint tìm thấy bất kỳ giải pháp thích hợp.Làm cách nào để nhận IP_ADDRESS ở định dạng IPV4
đây là mã mà tôi đang sử dụng để có được địa chỉ IP của một thiết bị
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;
}
Tôi nhận ouput này:
ip1--:/fe80::5054:ff:fe12:3456%eth0%2
ip2--:fe80::5054:ff:fe12:3456%eth0
Nó sẽ được hiển thị như thế này:
192.168.1.1
hãy giúp tôi ..
Đừng quên sự cho phép để ngăn chặn kết quả null : –
Vyacheslav
Vị trí và nội dung cần khai báo "ipv4" – CrazyMind