安卓得到外部IP [英] Android get external IP
本文介绍了安卓得到外部IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发的Android 2.1的应用程序,我想显示外部IP。我怎么能这样做?先谢谢了。
解决方案
公共无效getCurrentIP(){
ip.setText(请稍候...);
尝试 {
HttpClient的HttpClient的=新DefaultHttpClient();
HTTPGET HTTPGET =新HTTPGET(http://ifcfg.me/ip);
// HTTPGET HTTPGET =新HTTPGET(http://ipecho.net/plain);
HTT presponse响应;
响应= httpclient.execute(HTTPGET);
。//Log.i("externalip",response.getStatusLine()的toString());
HttpEntity实体= response.getEntity();
如果(实体!= NULL){
长的len = entity.getContentLength();
如果(LEN = -1放大器;!&安培; LEN< 1024){
字符串str = EntityUtils.toString(实体);
//Log.i("externalip",str);
ip.setText(STR);
} 其他 {
ip.setText(响应时间过长或错误。);
//调试
//ip.setText("Response太长或错误:+ EntityUtils.toString(实体));
//Log.i("externalip",EntityUtils.toString(entity));
}
} 其他 {
ip.setText(空:+ response.getStatusLine()的toString());
}
}
赶上(例外五)
{
ip.setText(错误);
}
}
I am developing an application in android 2.1 and I want to display the external IP. How could I do this? Thanks in advance.
解决方案
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");
}
}
这篇关于安卓得到外部IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文