//空气质量
@RequestMapping(value="/environment/sixPointer.action")
public ModelAndView sixPointer(HttpServletRequest request, HttpServletResponse responses) throws Exception {
responses.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
responses.setHeader("Access-Control-Allow-Credentials", "true");
responses.setHeader("P3P", "CP=CAO PSA OUR");
if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
responses.addHeader("Access-Control-Allow-Methods", "POST,GET,TRACE,OPTIONS");
responses.addHeader("Access-Control-Allow-Headers", "Content-Type,Origin,Accept");
responses.addHeader("Access-Control-Max-Age", "120");
}
//初始化
CloseableHttpClient httpClient=HttpClients.createDefault();
//天气接口,token为令牌,dailysteps表示需要7天的数据
String URL="https://api.seniverse.com/v3/air/hourly.json?key=SbPezfE5xitpdCWP6&language=zh-Hans&location=章丘";
//实例化httpGet,傳入URL参数
HttpGet httpGet=new HttpGet(URL);//HttpPost
// 设置请求报头,模拟Chrome浏览器
httpGet.addHeader("Accept", "application/json, text/javascript, */*; q=0.01");
// httpGet.addHeader("Accept-Encoding", "gzip,deflate,sdch");
httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
//设置编码
httpGet.addHeader("Content-Type", "text/html; charset=UTF-8");
//让浏览器记住访问的浏览器,可不写
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36");
// 发送GET请求
CloseableHttpResponse response=httpClient.execute(httpGet);
// 从HTTP响应中取出页面内容
HttpEntity myEntity= response.getEntity();
String res=EntityUtils.toString(myEntity);
// 打印出页面的内容:
JSONObject json = JSONObject.fromObject(res);
System.out.println(json);
Map info=new HashMap();
info.put("res",res);
//关闭连接
response.close();
httpClient.close();
outJsonObject(JsonUtil.toJson(json), responses);
return null;
}
HttpClient 调用 接口 心知天气 预报
最新推荐文章于 2023-04-14 12:46:14 发布