我想获取Android手机的IP地址,为此我尝试了InetAddress,但是我在仿真器上获得了IP地址127.0.0.1。我怎样才能得到实际的IP地址。 其次,我想通过使用Web服务器的IP地址联系该android移动设备,询问其位置等信息。 对于例如:假设Phone1需要Phone2的信息,则Phone1使用其保存的IP地址联系web服务器和web服务器联系人phone2,然后phone2响应其位置给web服务器和web服务器响应phone1。我想获取Android手机的IP地址,以及如何从Web服务器联系android手机使用该IP地址获取一些信息?
1
A
回答
0
此代码为越来越IPV4:
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress in`enter code here`etAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
if (inetAddress instanceof Inet4Address) {
return ((Inet4Address)inetAddress).getHostAddress().toString();
}
}
}
}
} catch (SocketException ex) {
Log.e("ServerActivity", ex.toString());
}
return null;
}
0
0
这里找到:http://www.droidnova.com/get-the-ip-address-of-your-device,304.html
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(LOG_TAG, ex.toString());
}
return null;
}
因此,如果此方法返回null,没有可用的连接。 如果该方法返回一个字符串,则该字符串包含独立于3G或WiFi的设备当前使用的IP地址。
相关问题
- 1. 如何获取使用Android手机的系统的IP地址?
- 2. 如何从IP地址获取服务器的机器名称?
- 3. 如何获取服务器IP地址?
- 4. 手机的IP地址是
- 5. 如何获得Android手机的IP地址编程....?
- 6. 获取实际机器的IP地址
- 7. 从ip地址获取信息
- 8. 如何在rails上获取主机服务器的IP地址
- 9. 从IP地址获取主机名
- 10. 从IP地址获取主机名
- 11. 从主机名获取服务器IP地址
- 12. 有没有办法根据手机号码从手机获取WAN IP地址?
- 13. 从服务器端获取已知MAC地址的IP地址
- 14. 获取服务器IP地址
- 15. 获取IP地址当前服务器
- 16. 获取服务器IP地址
- 17. 如何从CLI获取使用PHP的服务器IP地址?
- 18. 获取域的IP地址和后端服务器信息
- 19. 使用jQuery手机验证ip地址
- 20. 获取我的IP地址
- 21. 获取虚拟机的IP地址
- 22. 获取SFML中的主机IP地址
- 23. 获取打印机的IP地址
- 24. 获取IP地址
- 25. 获取IP地址
- 26. 获取IP地址
- 27. 从Android应用获取设备的MAC地址和IP地址
- 28. 在PHP中获取服务器的主机名或IP地址
- 29. 如何获取IP地址?
- 30. 如何获取IP地址?
此代码获取IPV4 – Narendra