Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用IP获取国家代码和国家名称_Java_Android - Fatal编程技术网

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

Java 如何使用IP获取国家代码和国家名称,java,android,Java,Android,我是android的新手,我想用ip地址获取国家名称和国家代码。请任何人引导我 下面是获取IP的代码: public String getLocalIpAddress() { WifiManager wifiMgr = (WifiManager) getActivity().getSystemService(context.WIFI_SERVICE); if(wifiMgr.isWifiEnabled()) { WifiInfo wifiInfo = wifiMg

我是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;
}
公共字符串getLocalIpAddress(){
WifiManager wifiMgr=(WifiManager)getActivity().getSystemService(context.WIFI_服务);
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));
返回wifiIpAddress;
}否则{
试一试{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
Log.i(“,”111 inetAddress.getHostAddress():“+inetAddress.getHostAddress());
//代码段中缺少&&之后的条件,正在检查inetAddress的实例
如果(!inetAddress.isLoopbackAddress()&&inetAddress instanceof Inet4Address){
Log.i(“,”111返回inetAddress.getHostAddress():“+inetAddress.getHostAddress());
返回inetAddress.getHostAddress();
}
}
}
}捕获(SocketException e){
e、 printStackTrace();
}
}
返回null;
}
我不知道如何使用IP地址获取国家代码和名称


提前谢谢

您可以使用此完美指南:


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

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

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


我希望这能有所帮助。

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

查一下图书馆

该图书馆使用所有人提供的官方数据库,这些数据库经常更新


我在过去为此目的使用API,我的服务经常达到API速率限制。IMHO提供了更大的灵活性,并消除了应用程序的外部依赖性。

如果不需要从IP地址查找国家代码和名称,那么最好的查找方式是基于SIM卡或网络,如果不使用SIM(平板电脑)方式。。我所做的。。。如何找到希望它能帮助你。。。
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();
    }
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();
}