AirQualityIndexUtil.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.huimv.common.utils;
  2. import net.sf.json.JSONObject;
  3. import java.io.*;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.net.URLEncoder;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. /**
  11. * @Author Anchor
  12. * @Date 2021/7/16 10:30
  13. * @Version 1.0.1
  14. */
  15. public class AirQualityIndexUtil {
  16. /**
  17. * 天气情况查询接口地址
  18. */
  19. public static String API_URL = "http://apis.juhe.cn/simpleWeather/query";
  20. /**
  21. * 请求key ---这个key是杨迪自己注册的.免费版.....希望公司后面买正式版
  22. */
  23. public static String API_KEY = "b9876f1d1eaec3ae987fcd54646060f7";
  24. /*public static void main(String[] args) {
  25. String cityName = "嘉兴";
  26. queryWeather(cityName);
  27. }*/
  28. /**
  29. * 根据城市名查询天气情况
  30. *
  31. * @param cityName
  32. */
  33. public static String queryWeather(String cityName) {
  34. //组合参数
  35. Map<String, Object> params = new HashMap<>();
  36. params.put("city", cityName);
  37. params.put("key", API_KEY);
  38. String queryParams = urlEncode(params);
  39. String response = doGet(API_URL, queryParams);
  40. System.out.println(response);
  41. try {
  42. JSONObject jsonObject = JSONObject.fromObject(response);
  43. int errorCode = jsonObject.getInt("error_code");
  44. if (errorCode == 0) {
  45. System.out.println("调用接口成功");
  46. JSONObject result = jsonObject.getJSONObject("result");
  47. JSONObject realtime = result.getJSONObject("realtime");
  48. /* System.out.printf("城市:%s%n", result.getString("city"));
  49. System.out.printf("天气:%s%n", realtime.getString("info"));
  50. System.out.printf("温度:%s%n", realtime.getString("temperature"));
  51. System.out.printf("湿度:%s%n", realtime.getString("humidity"));
  52. System.out.printf("风向:%s%n", realtime.getString("direct"));
  53. System.out.printf("风力:%s%n", realtime.getString("power"));
  54. System.out.printf("空气质量:%s%n", realtime.getString("aqi"));
  55. */
  56. return realtime.getString("aqi");
  57. } else {
  58. System.out.println("调用接口失败:" + jsonObject.getString("reason"));
  59. }
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. return null;
  64. }
  65. /**
  66. * get方式的http请求
  67. *
  68. * @param httpUrl 请求地址
  69. * @return 返回结果
  70. */
  71. public static String doGet(String httpUrl, String queryParams) {
  72. HttpURLConnection connection = null;
  73. InputStream inputStream = null;
  74. BufferedReader bufferedReader = null;
  75. //返回结果字符串
  76. String result = null;
  77. try {
  78. //创建远程url连接对象
  79. URL url = new URL(new StringBuffer(httpUrl).append("?").append(queryParams).toString());
  80. //通过远程url连接对象打开一个连接,强转成httpURLConnection类
  81. connection = (HttpURLConnection) url.openConnection();
  82. //设置连接方式:get
  83. connection.setRequestMethod("GET");
  84. //设置连接主机服务器的超时时间:5000毫秒
  85. connection.setConnectTimeout(5000);
  86. //设置读取远程返回的数据时间:6000毫秒
  87. connection.setReadTimeout(6000);
  88. //发送请求
  89. connection.connect();
  90. //通过connection连接,获取输入流
  91. if (connection.getResponseCode() == 200) {
  92. inputStream = connection.getInputStream();
  93. //封装输入流,并指定字符集
  94. bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
  95. //存放数据
  96. StringBuilder sbf = new StringBuilder();
  97. String temp;
  98. while ((temp = bufferedReader.readLine()) != null) {
  99. sbf.append(temp);
  100. sbf.append(System.getProperty("line.separator"));
  101. }
  102. result = sbf.toString();
  103. }
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. } finally {
  107. //关闭资源
  108. if (null != bufferedReader) {
  109. try {
  110. bufferedReader.close();
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. }
  114. }
  115. if (null != inputStream) {
  116. try {
  117. inputStream.close();
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. }
  121. }
  122. if (connection != null) {
  123. //关闭远程连接
  124. connection.disconnect();
  125. }
  126. }
  127. return result;
  128. }
  129. /**
  130. * 将map型转为请求参数型
  131. *
  132. * @param data
  133. * @return
  134. */
  135. public static String urlEncode(Map<String, ?> data) {
  136. StringBuilder sb = new StringBuilder();
  137. for (Map.Entry<String, ?> i : data.entrySet()) {
  138. try {
  139. sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
  140. } catch (UnsupportedEncodingException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. String result = sb.toString();
  145. result = result.substring(0, result.lastIndexOf("&"));
  146. return result;
  147. }
  148. }