当今购物、旅游等服务型的网站如此流行,我们有时候也会碰到这样网站的开发。
在开发此类网站时,为了增加用户的体验感受,我们不得不在用户刚进入网站时定位到用户所在地,
更好的为用户推荐当地产品。比如去哪儿,携程,美团等都会有定位功能。
那么我们怎样“贴心”的为用户定位呢?
1.首先我们需要先获取本机的外网ip
/**
* 得到本机的外网ip,出现异常时返回空串""
* @return
*/
public static String getPublicIP() {
String ip = "";
org.jsoup.nodes.Document doc = null; Connection con = null;
con = Jsoup.connect("http://www.ip138.com/ip2city.asp").timeout(10000);
try { doc = con.get();
// 获得包含本机ip的文本串:您的IP是:[xxx.xxx.xxx.xxx]
org.jsoup.select.Elements els = doc.body().select("center");
for (org.jsoup.nodes.Element el : els) {
ip = el.text();
}
// 从文本串过滤出ip,用正则表达式将非数字和.替换成空串""
ip = ip.replaceAll("[^0-9.]",
"");
} catch (IOException e) {
e.printStackTrace();
}
return ip;
}
2.定义一个方法,将字符拼接成字符串
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
3.将URL资源解析成json对象
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = null;
try {
is = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
//关闭输入流
is.close();
}
}
4.获取当地地址名,这个可以根据自己的具体需求更改。参数ak后面的值是ip定位api服务的密钥,
这个可以自己去百度申请,详见 http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm
public static String getAddrName() throws JSONException, IOException{
//这里调用百度的ip定位api服务 详见 http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm
JSONObject json = readJsonFromUrl("http://api.map.baidu.com/location/ip?ak=iTrwV0ddxeFT6QUziPQh2wgGofxmWkmg&ip="+getPublicIP());
/* 获取到的json对象:
* {"address":"CN|河北|保定|None|UNICOM|0|0",
* "content":{"address_detail":{"province":"河北省","city":"保定市","street":"","district":"","street_number":"","city_code":307},
* "address":"河北省保定市","point":{"x":"12856963.35","y":"4678360.5"}},
* "status":0}
*/
//这里我们可以输出json看一下具体格式
System.out.println(json.toString());
JSONObject content=json.getJSONObject("content"); //获取json对象里的content对象
JSONObject addr_detail=content.getJSONObject("address_detail");//从content对象里获取address_detail
String city=addr_detail.get("city").toString(); //获取市名,可以根据具体需求更改,如果需要获取省份的名字,可以把“city”改成“province”...
return city;
}
5.这里我们写一个主方法方便于测试
public static void main(String[] args) throws IOException, JSONException {
System.out.println(getAddrName());
}
运行结果:
