在我的应用程序中,我需要获取服务器 ip .. 我获取 ip 的代码是
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();
Log.v("","ip1--:" + inetAddress);
Log.v("", "ip2--:" + inetAddress.getHostAddress());
String ipv4;
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) {
String ip = inetAddress.getHostAddress().toString();
Toast.makeText(getApplicationContext(), inetAddress., Toast.LENGTH_SHORT).show();
Log.v("","ip---::" + ip);
// return inetAddress.getHostAddress().toString();
return ipv4;
}
}
}
但如果设备连接到联网的 wifi 设备,它会返回本地地址。请告诉我如何获取网络的父 ip。提前致谢...