【问题标题】:How to get country code and Country name using IP如何使用 IP 获取国家代码和国家名称
【发布时间】:2017-01-05 12:18:36
【问题描述】:

我是 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 地址获取国家代码和名称。

提前致谢!

【问题讨论】:

  • 如果不需要从 IP 地址查找国家代码和名称,而不是基于 SIM 或网络的最佳查找方式
  • 如果不使用 sim(Tablet) 意味着.. 我做什么... 如何找到
  • 希望对您有所帮助...stackoverflow.com/q/33516266/6334037

标签: java android


【解决方案1】:

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 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;
      }
    

    【讨论】:

      【解决方案3】:

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

      检查ip2asn2cc 库。

      本库使用所有Regional Internet Registries提供的官方数据库,经常更新。

      我过去曾为此目的使用 API,而我的服务非常频繁地达到 API 速率限制。恕我直言,ip2asn2cc 提供了更大的灵活性并消除了应用程序的外部依赖。

      【讨论】:

      • 这个库看起来很有趣,但没有真正的文档。查看代码,它似乎只允许您设置要过滤的包含或排除的国家代码列表。它不会为您提供有关 IP 映射到哪个国家/地区代码的一般信息。
      【解决方案4】:

      首先您应该获取设备的外部 IP 地址,然后使用一些 Web API like this 获取国家代码和名称。

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-06-23
          • 2016-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-13
          相关资源
          最近更新 更多