根据IP获取地理位置
2017-05-16 10:52
211 查看
依赖第三方接口,根据IP获取地理位置,Java实现代码如下:
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
//根据IP获取国家省份城市
public class IPAnalyse {
public static void main(String[] args) throws IOException {
String ip = "61.157.45.92";
String add[] = getAddressByIP1(ip);
System.out.println("国家:"+ add[0] +",省份:"+ add[1] +",城市:"+add[2]);
}
public static String[] getAddressByIP1(String strIP) throws IOException {
URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + strIP);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = null;
StringBuilder result = new StringBuilder();
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
String res = result.toString();
Gson gson = new Gson();
HashMap<String, Object> kv = gson.fromJson(res, HashMap.class);
LinkedTreeMap map = (LinkedTreeMap) kv.get("data");
String[] location = new String[3];
location[0] = ""; //国家
location[1] = ""; //省份
location[2] = ""; //市区
if ((map != null) && (!map.isEmpty())) {
location[0] = (String) map.get("country");
location[1] = (String) map.get("region");
location[2] = (String) map.get("city");
}
return location;
}
}
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
//根据IP获取国家省份城市
public class IPAnalyse {
public static void main(String[] args) throws IOException {
String ip = "61.157.45.92";
String add[] = getAddressByIP1(ip);
System.out.println("国家:"+ add[0] +",省份:"+ add[1] +",城市:"+add[2]);
}
public static String[] getAddressByIP1(String strIP) throws IOException {
URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + strIP);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = null;
StringBuilder result = new StringBuilder();
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
String res = result.toString();
Gson gson = new Gson();
HashMap<String, Object> kv = gson.fromJson(res, HashMap.class);
LinkedTreeMap map = (LinkedTreeMap) kv.get("data");
String[] location = new String[3];
location[0] = ""; //国家
location[1] = ""; //省份
location[2] = ""; //市区
if ((map != null) && (!map.isEmpty())) {
location[0] = (String) map.get("country");
location[1] = (String) map.get("region");
location[2] = (String) map.get("city");
}
return location;
}
}
相关文章推荐
- java脚本开发根据客户IP获取IP的具体地理位置信息
- 根据IP获取地理位置
- php根据ip获取地理位置
- 纯真IP库实现根据IP获取地理位置
- php根据IP获取用户地理位置
- java根据ip获取地理位置
- Node.js和PHP根据ip获取地理位置的方法
- Java 实现根据 IP 地址获取地理位置的代码分享
- java根据url获取json对象 java根据ip获取地理位置
- PHP安装GeoIP扩展根据IP获取地理位置及计算距离的方法
- java根据ip地质调用百度定位api服务获取地理位置示例
- Node.js和PHP根据ip获取地理位置的方法
- ASP.NET2.0中根据IP来获取客户的地理位置
- PHP安装GeoIP扩展根据IP获取地理位置及计算距离的方法
- paip.获取地理位置根据Ip
- 根据IP获取用户的地理位置
- Java获取客户端IP地址、MAC地址,根据IP获取地理位置信息代码
- paip.获取地理位置根据Ip
- Android根据IP获取地理位置,精确到经纬度
- 根据ip获取地理位置