【发布时间】:2019-10-21 17:26:01
【问题描述】:
我可以使用设备 SIM 卡的位置从设备获取国家/地区名称,但对于没有 SIM 卡的平板电脑,这将不起作用。当设备连接到互联网时,如何从设备的 ip 中获取国家代码或国家名称?
【问题讨论】:
标签: android
我可以使用设备 SIM 卡的位置从设备获取国家/地区名称,但对于没有 SIM 卡的平板电脑,这将不起作用。当设备连接到互联网时,如何从设备的 ip 中获取国家代码或国家名称?
【问题讨论】:
标签: android
这是一个适用于 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();
}
});
【讨论】:
这取决于您想要达到的精确度 - 请注意,这种技术无论如何都不是很精确!有各种各样的方法可以通过 IP 地址打败地理定位。
但是,有些网站可以用 IP 地址查询,它会返回一个国家/地区。
但是,如果您不想上网查找,则需要转储当前列表,将其编码到查找表中,然后将其存储在程序中。
您不会想要存储每个地址 - 有 40 亿个可能的地址 - 而是存储大范围。这将大大减少表格的大小。
【讨论】: