Android手机获取外网ip(by 星空武哥)

               

转载请标注原创地址:http://blog.csdn.net/lsyz0021/article/details/51234178


     今天有个朋友要获取Android手机当前连入网络的ip,问我怎么做,我一想这还不简单。告诉他先判断是什么网络环境,如果是WiFi可以通过WifiManager获取到,如果是流量(2G、3G或者4G网)就通过NetworkInterface遍历获取getHostAddress()获得,但是他们要求获取不是路由器发出的局域网ip,而是当前的外网ip,一般我们手机连接路由器,路由器分给我们的ip都是路由器转发的C网段的局域网ip,也就是192.168.x.xx  这样的网段,但是我们想要的真实的外网ip怎么获取呢?

    首先,打开百度输入“ip”,你会发现他会显示你当前电脑连接的外网ip,也就是你的真实ip。



     百度是可以通过搜索“ip”就能显示我们想要 的外网ip的,但是我们android代码怎么获取呢?难道我们就携带必要的请求参数,通过get请求百度然后再通过结果来获取我们要的信息吗?这个请求的页面数据比较多,有没有更简洁的结果呢???

通过点击“IP地址查询”的超链接,你会发现……



     这个Url (http://www.ip138.com/也可以获取到我们想要的ip,那么这个页面的信息还是有点多,通过查看该页面的源代码我们发现……



    这个页面其实也是通过请求另一个地址(http://1212.ip138.com/ic.asp)来获取的,打开该页面,我们发现返回的数据好简洁哦!


     通过查看该页面的源代码发现……我们可以获取字符的“[]”标志,来获取我们要的ip信息的



    通过上面的分析我们最终找到了地址 http://1212.ip138.com/ic.asp ,通过这个地址我们就能获取我们要的ip。那么接下来就要界面该页面的返回的数据了。


写了一个简单的工具类

/** * Created by Lilu */public class IPUtil {    /**     * 获取外网的IP(必须放到子线程里处理)     */    public static String getNetIp() {        String ip = "";        InputStream inStream = null;        try {            URL infoUrl = new URL("http://1212.ip138.com/ic.asp");            URLConnection connection = infoUrl.openConnection();            HttpURLConnection httpConnection = (HttpURLConnection) connection;            int responseCode = httpConnection.getResponseCode();            if (responseCode == HttpURLConnection.HTTP_OK) {                inStream = httpConnection.getInputStream();                BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "gb2312"));                StringBuilder builder = new StringBuilder();                String line = null;                while ((line = reader.readLine()) != null) {                    builder.append(line);                    //builder.append(line).append("\n");                }                inStream.close();                int start = builder.indexOf("[");                int end = builder.indexOf("]");                ip = builder.substring(start + 1, end);                return ip;            }        } catch (IOException e) {            e.printStackTrace();        }        return null;    }}

因为用到了网络请求,不要忘了添加网络权限

<uses-permission android:name="android.permission.INTERNET" />

简单的用TextView显示了我们获取的外网ip

public class MainActivity extends AppCompatActivity {    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv = (TextView) findViewById(R.id.tv);        new Thread(new Runnable() {            @Override            public void run() {                // 获取外网ip                final String ip = IPUtil.getNetIp();                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        tv.setText("ip = "+ip);                    }                });            }        }).start();    }}




拿出微信 扫码关注下面的微信订阅号,及时获取更多推送文章



           

猜你喜欢

转载自blog.csdn.net/qq_44934581/article/details/89485720
今日推荐