首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用IP获取国家代码和国家名称

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

Stack Overflow用户
提问于 2016-08-29 20:47:03
回答 5查看 10.3K关注 0票数 1

我是新的android,我想获得国家名称和国家代码使用ip地址。请任何人指导我。

获取IP的代码如下:

代码语言:javascript
复制
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地址获取国家代码和名称。

提前感谢!

EN

回答 5

Stack Overflow用户

发布于 2016-08-30 00:35:55

1.)查询您的公网ip地址:

代码语言:javascript
复制
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.)然后查询您的国家代码/名称:(使用上面的方法)

代码语言:javascript
复制
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();
}

我希望这能帮到你。

票数 3
EN

Stack Overflow用户

发布于 2016-08-29 20:56:22

你可以使用这个完美的指南:http://www.mkyong.com/java/java-find-location-using-ip-address/

代码语言:javascript
复制
  //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
EN

Stack Overflow用户

发布于 2016-08-30 00:00:54

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39206727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档