【问题标题】:How To Know Country name from device ip address?如何从设备 IP 地址知道国家名称?
【发布时间】:2019-10-21 17:26:01
【问题描述】:

我可以使用设备 SIM 卡的位置从设备获取国家/地区名称,但对于没有 SIM 卡的平板电脑,这将不起作用。当设备连接到互联网时,如何从设备的 ip 中获取国家代码或国家名称?

【问题讨论】:

    标签: android


    【解决方案1】:

    这是一个适用于 Android 的 GeoIP 库,您可以在项目中使用。 它使用在线 API 从 IP 地址获取国家/地区。

    https://github.com/seventhmoon/GeoIp-android

    这是一个关于如何使用它的sn-p:

       ApiManager apiManager = new ApiManager(Volley.newRequestQueue(context));
       apiManager.getGeoIpInfo(new Response.Listener<GeoIpResponseModel>() {
            @Override
            public void onResponse(GeoIpResponseModel response) {
                //This is how you get the information.
                //not all attribute are listed
                String country = response.getCountry();
                String city = response.getCity();
                String countryCode = resopnse.getCountryCode();
                double latitude = response.getLatitude();
                double longtidue = response.getLongitude();
                String region = response.getRegion();
                String timezone = response.getTimezone();
                String isp = response.getIsp();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                String errorMessage = error.toString();
            }
        });
    

    【讨论】:

    • 感谢marmor,我已经使用模拟器测试了这段代码,但结果为空。
    • 我用真机检查过还是不行!!显示空值。
    • 每天限制 1000 个请求,在此处查看更多信息ipapi.co/#pricing
    【解决方案2】:

    这取决于您想要达到的精确度 - 请注意,这种技术无论如何都不是很精确!有各种各样的方法可以通过 IP 地址打败地理定位。

    但是,有些网站可以用 IP 地址查询,它会返回一个国家/地区。

    但是,如果您不想上网查找,则需要转储当前列表,将其编码到查找表中,然后将其存储在程序中。

    您不会想要存储每个地址 - 有 40 亿个可能的地址 - 而是存储大范围。这将大大减少表格的大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2016-09-03
      • 2012-06-28
      • 2016-02-04
      • 1970-01-01
      相关资源
      最近更新 更多