//通过IP得到省市
public static Area getIpArea(String ip){
Area area = new Area();
String province = "";
String city = "";
for(int i = 0;i<3;i++){
try {
String path = "http://ip.taobao.com/service/getIpInfo.php?ip="+ip;
String inputline="";
String info="";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(10*1000);
conn.setRequestMethod("GET");
int code =conn.getResponseCode();
System.out.println(code);
InputStreamReader inStream = new InputStreamReader(conn.getInputStream(),"UTF-8");
BufferedReader buffer=new BufferedReader(inStream);
while((inputline=buffer.readLine())!=null){
info+=inputline;
}
JSONObject jsonob = JSONObject.fromObject((JSONObject.fromObject(info).getString("data")));
province = StringEscapeUtils.escapeSql(jsonob.getString("region"));
city = StringEscapeUtils.escapeSql(jsonob.getString("city"));
if(code==200){
area.setProvince(province);
area.setCity(city);
break;
}
} catch (IOException e) {
i++;
}
}
if("".equals(province)||province==null){
area.setProvince("其他");
}
if("".equals(city)||city==null){
area.setCity("其他");
}
return area;
}
用淘宝接口通过ip查询地域
最新推荐文章于 2019-05-15 10:49:56 发布