如何使用IP获取国家代码和国家名称

3

我是一个 Android 新手,想要通过 IP 地址获取国家名称和国家代码。请有经验的人指导一下。

以下代码可用于获取 IP:

public String getLocalIpAddress() {
    WifiManager wifiMgr = (WifiManager) getActivity().getSystemService(context.WIFI_SERVICE);
    if(wifiMgr.isWifiEnabled()) {
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        int ip = wifiInfo.getIpAddress();
        String wifiIpAddress = String.format("%d.%d.%d.%d",
                (ip & 0xff),
                (ip >> 8 & 0xff),
                (ip >> 16 & 0xff),
                (ip >> 24 & 0xff));

        return wifiIpAddress;
    }else{
        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();
                    Log.i("","111 inetAddress.getHostAddress(): "+inetAddress.getHostAddress());
                    //the condition after && is missing in your snippet, checking instance of inetAddress
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                        Log.i("","111 return inetAddress.getHostAddress(): "+inetAddress.getHostAddress());
                        return inetAddress.getHostAddress();
                    }

                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    return null;
}

我不知道如何使用IP地址获取国家代码和名称。
提前感谢!

3
如果不需要根据IP地址查找国家代码和名称,则基于SIM卡或网络查找是最好的方法。 - undefined
如果不使用sim(平板电脑)意味着...我该怎么办...如何找到? - undefined
希望能对你有所帮助... http://stackoverflow.com/q/33516266/6334037 - undefined
5个回答

4

1.) 查询您的公共IP地址:

public static String getPublicIP() throws IOException
    {
        Document doc = Jsoup.connect("http://www.checkip.org").get();
        return doc.getElementById("yourip").select("h1").first().select("span").text();
    }

2.) 然后查询您的国家代码/名称:(使用上述方法)

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://ipinfo.io/"+getPublicIP());
HttpResponse response;
try {
    response = client.execute(request);

    Log.d("Response of GET request", response.toString());
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
} catch (IOException e) {
      // TODO Auto-generated catch block
     e.printStackTrace();
}

我希望这可以有所帮助。

2
你可以使用这个完美的指南:http://www.mkyong.com/java/java-find-location-using-ip-address/。它可以帮助你通过 IP 地址查找位置,非常适用于 IT 技术相关工作。
  //ip address something like that 192.168.0.1
  public String getCountryFromIP(String ipAddress) {

    File file = new File("resources/GeoLiteCity.dat");

    LookupService lookup = new LookupService(file,LookupService.GEOIP_MEMORY_CACHE);
    Location locationServices = lookup.getLocation(ipAddress);

    return locationServices.countryName;
  }

1

有一种比使用第三方API和经常过时的GeoLiteCity数据库更好的方法。

请检查ip2asn2cc库。

该库使用所有地区互联网注册机构提供的官方数据库,这些数据库经常得到更新。

我过去使用API来完成此任务,但我的服务经常受到API速率限制的影响。在我看来,ip2asn2cc提供了更多的灵活性,并消除了应用程序中的外部依赖。


这个库看起来很有趣,但是没有真正的文档。浏览代码后,似乎它只允许您设置一个包含或排除国家代码列表以进行过滤。它不会提供有关IP映射到哪个国家代码的一般信息。 - undefined
@idolize 它确实可以,并且非常简单易用!ip2asn2cc从RIR(区域互联网注册机构)获取一个更新的子网列表和它们所属的国家(分配)的列表。您可以通过查看这里的测试来了解如何使用ip2asn2cc:https://github.com/AxLabs/ip2asn2cc/blob/master/src/test/java/com/axlabs/ip2asn2cc/Ip2Asn2CcIncludeFilterPolicyTest.java#L26-L33 - undefined

0
首先,您应该获取设备的外部IP地址,然后使用一些网络API(例如这样的)来获取国家代码和名称。

0
在我的一个应用程序中,我有一个相同的需求,根据IP地址获取用户位置,因为在Android手机上无法获取外部IP地址。所以我们在服务器端实现了一个Web服务来实现它。通过我们的Web服务,我们能够获取用户的外部IP地址,然后我们使用Maxmind API来获取用户的国家代码和名称。Maxmind API是付费的,但很容易实现。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接