AirQualityIndexUtil.java 5.4 KB

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