我正在开发android 2.1中的应用程序,我想显示外部IP。我怎么能这样做?提前致谢。Android获取外部IP
1
A
回答
14
public void getCurrentIP() {
ip.setText("Please wait...");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://ifcfg.me/ip");
// HttpGet httpget = new HttpGet("http://ipecho.net/plain");
HttpResponse response;
response = httpclient.execute(httpget);
//Log.i("externalip",response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
if (len != -1 && len < 1024) {
String str=EntityUtils.toString(entity);
//Log.i("externalip",str);
ip.setText(str);
} else {
ip.setText("Response too long or error.");
//debug
//ip.setText("Response too long or error: "+EntityUtils.toString(entity));
//Log.i("externalip",EntityUtils.toString(entity));
}
} else {
ip.setText("Null:"+response.getStatusLine().toString());
}
}
catch (Exception e)
{
ip.setText("Error");
}
}
1
我不认为有办法以编程方式执行它,但你可以调用一个像http://www.whatismyip.com/这样的站点,然后从页面中去除IP。您可能希望找到提供API并支持第三方调用的网站。
2
http://api.externalip.net/ip将返回你的IP在简单的API格式
您可以在此处详细了解获得外部IP:http://www.externalip.net/api
-2
看看这个代码片段:
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) {
ex.printStackTrace();
}
Log.e("IP ADDRESS:", ipAddress);
+0
获取本地IP而非外部IP – inVINCEable 2014-12-22 18:32:37
相关问题
- 1. 获取外部IP
- 2. 在Java中获取'外部'IP地址
- 3. 获取外部IP地址10
- 4. Android - 从外部获取onActivityResult
- 5. 获取非HTTP流量到OpenShift Origin(入口IP /外部IP)?
- 6. Sinatra外部IP
- 7. 从Android的热点IP获取IP
- 8. Android 4.0.3获取本地IP
- 9. Android获取外部UI组件句柄
- 10. Android访问获取外部目录
- 11. 获取例外:在Android小部件
- 12. 获取外部SD卡在Android手机
- 13. 在Android中获取外部SDCard位置
- 14. 在3G的Android RTSP使用外部IP
- 15. 如何从Java中Ping外部IP Android
- 16. 如何获得外部IP成功
- 17. 我如何获得我的外部/ IP?
- 18. 获取外部CGRect
- 19. C#TcpListener外部IP
- 20. Gcloud,Relase外部IP
- 21. 如何使用node.js获取我的外部IP地址?
- 22. 如何在目标c中获取外部IP
- 23. 使用kubectl或kubernetes API来获取服务的外部IP
- 24. 如何在局域网中获取外部IP地址?
- 25. 使用Python获取计算机的外部IP地址
- 26. 如何在Python中获取套接字的外部IP?
- 27. 从http://www.whatismyip.com/automation/n09230945.asp使用Adobe AIR获取外部IP
- 28. 获取主机的所有外部IP地址
- 29. 如何在Delphi中获取外部(公共)IP
- 30. 如何在C++中获取外部IP地址?
你认为什么是“外部”IP地址? – Squonk 2011-05-20 21:09:31