android如何获取本机ip地址及ip归属地

    2026-02-05 17:41:20

    前言

    本篇文章主要介绍android如何获取本机ip地址及ip归属地。

    定义

    ip地址是指手机在连接到互联网时所获得的唯一网络地址。

    ip归属地是指通过ip地址查询器对应的地理位置信息,如省市区等信息。

    获取ip地址

    如果只是查看本机ip,不涉及应用开发,可以依次打开手机设置-我的设备-状态信息-ip地址界面进行查看(不同品牌手机型号会有差异)。

    下面开发过程中获取本机ip方法:

    1.首先是要在清单文件中配置必要的权限:

    2.手机在不同的网络环境下获取ip的方法:

    //获取ip

    public void getIPAddress(Context context) {

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = cm.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {

    //网络连接可用,判断网络连接类型

    if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {

    //wifi网络

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

    int ipAddress = wifiInfo.getIpAddress();

    String ip = String.format("%d.%d.%d.%d",

    (ipAddress & 0xff),

    (ipAddress >> 8 & 0xff),

    (ipAddress >> 16 & 0xff),

    (ipAddress >> 24 & 0xff));

    Log.e("tag", "ip:" + ip);

    } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {

    //移动网络

    getLocalIpAddress();

    }

    } else {

    //没有网络链接

    }

    }

    private void getLocalIpAddress() {

    try {

    ArrayList networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());

    for (NetworkInterface networkInterface : networkInterfaces) {

    ArrayList inetAddresses = Collections.list(networkInterface.getInetAddresses());

    for (InetAddress address : inetAddresses) {

    String ip = address.getHostAddress();

    if (!address.isLoopbackAddress() && (address instanceof Inet4Address)) {

    Log.e("tag", "ipv4:" + ip);

    }

    if (!address.isLoopbackAddress() && (address instanceof Inet6Address)) {

    Log.e("tag", "ipv6:" + ip);

    }

    }

    }

    } catch (SocketException socketException) {

    Log.e("tag", socketException.getMessage());

    }

    }

    获取ip归属地

    想要获取到ip归属地,一般需要获取到ip地址后通过第三方服务来查询,下面展示一下android使用ip数据云获取ip归属地的具体方法:

    //获取ip归属地

    private Location getIpData(String ip, String key) {

    Location location = null;

    try {

    URL url = new URL("https://api.ipdatacloud.com/v2/query?ip=" + ip + "&key=+" + key);

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    StringBuffer sb = new StringBuffer();

    String line;

    while ((line = in.readLine()) != null) {

    sb.append(line);

    }

    in.close();

    connection.disconnect();

    // 解析返回的JSON数据,获取IP归属地信息

    // 这里需要使用JSON解析库,例如gson、fastjson

    String jsonResult = sb.toString();

    location = new Gson().fromJson(jsonResult, Location.class);

    } catch (Exception e) {

    e.printStackTrace();

    }

    return location;

    }

    class Location {

    private String AreaCode; //行政区码

    private String City; //城市

    private String CityCode; //城市代码

    private String Continent; //洲

    private String Country; //国家/地区

    private String CountryCode; //国家/地区英文简写

    private String District; //区县

    private String Elevation; //海拔

    private String Ip; //ip地址

    private String Isp; //运营商

    private String Latitude; //纬度

    private String Longitude; //经度

    private Street[] MultiStreet; //历史街道位置

    private String Province; //省份

    private String Street; //街道

    private String TimeZone; //时区

    private String WeatherStation; //气象站

    private String ZipCode; //邮编

    }

    class Street {

    private String Lng; //经度

    private String Lat; //纬度

    private String Province; //省份

    private String City; //城市

    private String District; //区县

    private String Street; //街道

    private String Radius; //范围半径

    private String ZipCode; //邮政编码

    }

    总结

    本文简要总结了android获取ip地址及归属地的方法,在实际开发中还需要根据自身的实际情况进行修改。

    在众多第三方服务中,ip数据云作为新一代ip地址数据服务领军者,为广大开发者提供了丰富的产品服务,具体可去官网https://www.ipdatacloud.com/?utm-source=WZJ&utm-keyword=?2811进行测试、咨询。