如何获取 IPV4 格式的 IP_ADDRESS [英] How do I get IP_ADDRESS in IPV4 format

查看:23
本文介绍了如何获取 IPV4 格式的 IP_ADDRESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取设备的 IP 地址,即使用 WIFI 或 3G 连接.我正在获取 IPV6 格式的 IP 地址,这是无法理解的.我想要 IPV4 格式的 IP 地址.我已经用谷歌搜索过,但没有找到任何合适的解决方案.

I am trying to get the IP address of an device i.e using WIFI or 3G connection. I am getting the ip address in IPV6 format which is not understandable. I want in IPV4 format IP address.I have done google but dint found any proper solutions.

这是我用来获取设备 IP 地址的代码

here is code which I am using to get IP address of an device

public String getLocalIpAddress() {
    try {
        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();
                System.out.println("ip1--:" + inetAddress);
                System.out.println("ip2--:" + inetAddress.getHostAddress());
                if (!inetAddress.isLoopbackAddress()) {


                    String ip = inetAddress.getHostAddress().toString();
                    System.out.println("ip---::" + ip);
                    EditText tv = (EditText) findViewById(R.id.ipadd);
                    tv.setText(ip);
                    return inetAddress.getHostAddress().toString();

                }
            }
        }
    } catch (Exception ex) {
        Log.e("IP Address", ex.toString());
    }
    return null;
}

我得到这个输出:

ip1--:/fe80::5054:ff:fe12:3456%eth0%2
ip2--:fe80::5054:ff:fe12:3456%eth0

它应该像这样显示:

192.168.1.1

请帮帮我..

推荐答案

尝试了很多技巧..终于可以得到IPV4格式的IP地址..这是我的代码..

After trying many tricks.. finally I can get the IP address in IPV4 format.. Here is my code..

public String getLocalIpAddress() {
    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();
                System.out.println("ip1--:" + inetAddress);
                System.out.println("ip2--:" + inetAddress.getHostAddress());

      // for getting IPV4 format
      if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) {

                    String ip = inetAddress.getHostAddress().toString();
                    System.out.println("ip---::" + ip);
                    EditText tv = (EditText) findViewById(R.id.ipadd);
                    tv.setText(ip);
                    // return inetAddress.getHostAddress().toString();
                    return ip;
                }
            }
        }
    } catch (Exception ex) {
        Log.e("IP Address", ex.toString());
    }
    return null;
}

添加了if条件,如下图

 /**This shows IPV4 format IP address*/
 if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())){}

代替这个

 /**This shows IPV6 format IP address*/
 if (!inetAddress.isLoopbackAddress()){}

非常感谢..拉胡尔

检查地址是否为第 4 版地址的替代方法是:

An alternative for checking if the address is a version 4 address is:

if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address)

这篇关于如何获取 IPV4 格式的 IP_ADDRESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆