首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android获取外网IP

Android获取外网IP
EN

Stack Overflow用户
提问于 2011-05-21 05:01:27
回答 4查看 22.2K关注 0票数 1

我正在开发一个android 2.1的应用程序,我想显示外部IP。我怎么能这样做呢?提前谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-06-03 02:40:36

代码语言:javascript
复制
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");
    }

}
票数 14
EN

Stack Overflow用户

发布于 2012-01-21 21:12:39

http://api.externalip.net/ip会以简单的接口格式返回您的ip

您可以在此处阅读有关获取外部ip的更多信息:http://www.externalip.net/api

票数 2
EN

Stack Overflow用户

发布于 2011-05-21 05:45:29

我不认为有一种方法可以通过编程来做到这一点,但你可以调用一个像http://www.whatismyip.com/这样的网站,然后从页面上剥离IP。您可能希望找到一个提供API并支持第三方呼叫的站点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6077555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档