来自 Andrey 链接,这里是如何构建查询,此代码将返回一个 HTML 文件,其中包含当前 IP 的所有详细信息,包括城市;
String IP= "123.123.123.123";
URL link = new URL("http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress="+IP);
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
in.close();
更新,2013 年 5 月 23 日
之前的答案是好的,但它不是 API 调用,它读取我之前提供的 HTML 页面,因为我没有找到任何免费的 API。接下来是一个 REST API 调用,可以轻松使用,并将返回所需的所有信息,建议使用这个:
String ip = "2.51.255.200";
URL url = new URL("http://freegeoip.net/csv/" + ip);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
int status = connection.getResponseCode();
if (status != 200) {
return null;
}
reader = new BufferedReader(new InputStreamReader(is));
for (String line; (line = reader.readLine()) != null;) {
//this API call will return something like:
"2.51.255.200","AE","United Arab Emirates","03","Dubai","Dubai","","x-coord","y-coord","",""
// you can extract whatever you want from it
}
From Andrey link, here is how to construct the inquiry, this code will return an HTML file with all details of current IP, including city;
String IP= "123.123.123.123";
URL link = new URL("http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress="+IP);
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
in.close();
UPDATE, 23 May 2013
The previous answer is ok, but it's not an API call, it reads an HTML page, that I provided previously because I didn't find any free APIs. Next is a REST API call that can be used easily and will return all the info required, it's recommended to use this one:
String ip = "2.51.255.200";
URL url = new URL("http://freegeoip.net/csv/" + ip);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
int status = connection.getResponseCode();
if (status != 200) {
return null;
}
reader = new BufferedReader(new InputStreamReader(is));
for (String line; (line = reader.readLine()) != null;) {
//this API call will return something like:
"2.51.255.200","AE","United Arab Emirates","03","Dubai","Dubai","","x-coord","y-coord","",""
// you can extract whatever you want from it
}
发布评论
评论(2)
来自 Andrey 链接,这里是如何构建查询,此代码将返回一个 HTML 文件,其中包含当前 IP 的所有详细信息,包括城市;
更新,2013 年 5 月 23 日
之前的答案是好的,但它不是 API 调用,它读取我之前提供的 HTML 页面,因为我没有找到任何免费的 API。接下来是一个 REST API 调用,可以轻松使用,并将返回所需的所有信息,建议使用这个:
From Andrey link, here is how to construct the inquiry, this code will return an HTML file with all details of current IP, including city;
UPDATE, 23 May 2013
The previous answer is ok, but it's not an API call, it reads an HTML page, that I provided previously because I didn't find any free APIs. Next is a REST API call that can be used easily and will return all the info required, it's recommended to use this one:
如果您的应用程序部署在防火墙后面。因此,我们可以使用 GeoLite,而不是调用 API,下面是示例代码。
从 http://geolite.maxmind.com/ 下载 City.dat 文件下载/geoip/database/GeoLiteCity.dat.gz
If your Application is deployed behind the firewall. So instead of calling a API, we can use GeoLite below is the sample code.
Download City.dat file from http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz