java 连接到3G移动网络时获取android设备的IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5818725/
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
Getting the IP address of the android device when connected to 3G mobile network
提问by Shahtaj
When I am connected to WiFi, I can obtain the IP address of the Android phone.
当我连接到WiFi时,我可以获得Android手机的IP地址。
However, when on the mobile network like 3G connection, is it still possible to obtain the IP address of the Android phone?
If yes, kindly post the code for the same.
但是,在3G连接等移动网络上,是否还能获取到Android手机的IP地址?
如果是,请发布相同的代码。
回答by Josnidhin
try something like this
尝试这样的事情
String ipAddress = null;
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()) {
ipAddress = inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {}
回答by Houssam Hamdan
the mobile device does not have an ip when browsing over 3G connection, You will get the ISP ip on the server side code. I recommend you replace the ip with the unique id, device type and coordinates if possible.
移动设备在通过 3G 连接浏览时没有 ip,您将在服务器端代码上获得 ISP ip。如果可能,我建议您将 ip 替换为唯一的 id、设备类型和坐标。