繁体   English   中英

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

[英]How to get country code and Country name using IP

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

提前致谢!

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

我希望这会有所帮助。

你可以使用这个完美的指南: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;
  }

首先你应该得到设备的外部 IP 地址,然后使用一些像这样的web API 来获取国家代码和名称。

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

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

检查ip2asn2cc库。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM