java教程

java 利用百度地图API获取当前城市名

位置:首页 > java教程 > java技巧,2018-08-10 20:56
开发中用到了根据IP地址获取用户当前所在城市名,下面是java 利用百度地图API获取当前城市名的方法

获取IP地址


public static String getIpAddr() {
	String ipAddress = "";
	// ipAddress = this.getRequest().getRemoteAddr();
	HttpServletRequest res = ServletActionContext.getRequest();
	ipAddress = res.getHeader("x-forwarded-for");
	if (ipAddress == null || ipAddress.length() == 0
			|| "unknown".equalsIgnoreCase(ipAddress)) {
		ipAddress = res.getHeader("Proxy-Client-IP");
	}
	if (ipAddress == null || ipAddress.length() == 0
			|| "unknown".equalsIgnoreCase(ipAddress)) {
		ipAddress = res.getHeader("WL-Proxy-Client-IP");
	}
	if (ipAddress == null || ipAddress.length() == 0
			|| "unknown".equalsIgnoreCase(ipAddress)) {
		ipAddress = res.getRemoteAddr();
		if (ipAddress.equals("127.0.0.1")) {
			// 根据网卡取本机配置的IP
			InetAddress inet = null;
			try {
				inet = InetAddress.getLocalHost();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
			ipAddress = inet.getHostAddress();
		}

	}

	// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
	if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
														// = 15
		if (ipAddress.indexOf(",") > 0) {
			ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
		}
	}
	
	

	
	return ipAddress;
}


java http网络请求


 public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(),"utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("GET请求出现异常" + e);
            e.printStackTrace();
        }
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            
        }
        return result;
    }

百度地图api请求

//IP可选
    	String url = "http://api.map.baidu.com/location/ip?ak=xxxx&ip=182.88.141.201&coor=bd09ll";
		     String params =null;
	      String requestResult=decode(sendGet(url,params));
	      net.sf.json.JSONObject json =net.sf.json.JSONObject.fromObject(requestResult);
			if (json.containsKey("status")) {// 正常返回
				if ("0".equals(json.getString("status"))) {
					System.out.println(requestResult);
					net.sf.json.JSONObject temp2 = net.sf.json.JSONObject.fromObject(json.getString("content"));
					net.sf.json.JSONObject temp3 = net.sf.json.JSONObject.fromObject(temp2.getString("address_detail"));
		      System.out.println(temp3.getString("city"));
				}}
注意,ak是百度地图的一个key,可在百度地图中免费获取。

以上就是java 利用百度地图API获取当前城市名的方法

TAGS:百度地图API获取当前城市名java 获取当前城市

猜你喜欢

NewHot