我正在开发一个android 2.1的应用程序,我想显示外部IP。我怎么能这样做呢?提前谢谢。
发布于 2011-06-03 02:40:36
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");
}
}发布于 2012-01-21 21:12:39
http://api.externalip.net/ip会以简单的接口格式返回您的ip
您可以在此处阅读有关获取外部ip的更多信息:http://www.externalip.net/api
发布于 2011-05-21 05:45:29
我不认为有一种方法可以通过编程来做到这一点,但你可以调用一个像http://www.whatismyip.com/这样的网站,然后从页面上剥离IP。您可能希望找到一个提供API并支持第三方呼叫的站点。
https://stackoverflow.com/questions/6077555
复制相似问题