Reputation: 537
Im new for android, I want to get country name and country code using ip address. Please anyone guide me.
Below code for getting 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;
}
I dont know how to get country code and name using IP address.
Thanks in advance!
Upvotes: 3
Views: 16944
Reputation: 197
There's a better way than using third-party APIs and the frequently outdated GeoLiteCity database.
Check the ip2asn2cc library.
This library uses official databases provided by all Regional Internet Registries, which are frequently updated.
I was using APIs for this purpose in the past, and my service was hitting API rate limits very frequently. IMHO, ip2asn2cc provides more flexibility and cuts out external dependencies out of applications.
Upvotes: 1
Reputation: 5468
1.) query your public ip-address:
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.) Then query your country code/name: (by using the method above)
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();
}
I hope this helps.
Upvotes: 5
Reputation: 504
In one of my application I have same requirement to get user location based on ip address, as in android phone we can not get the external IP address. So we implement it at our server side by implementing web service. Through our web service we were able to get the user external ip address and then we used Maxmind API to get user country code and name. Maxmind Api's are paid but easy to implement.
Upvotes: 0
Reputation: 321
First you should get external IP address of the device, and then use some web API like this to get country code and name.
Upvotes: 0
Reputation: 1032
You may use this perfect guide: 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;
}
Upvotes: 2