天气预警

master
chaiyapeng 2024-08-30 19:06:06 +08:00
parent f8dccc7350
commit 945b9beef0
6 changed files with 8175 additions and 27 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,7 @@
package com.muyu.domain; package com.muyu.domain;
import lombok.Data;
/** /**
* @Authorchaiyapeng * @Authorchaiyapeng
* @Packagecom.muyu.domain * @Packagecom.muyu.domain
@ -8,5 +10,10 @@ package com.muyu.domain;
* @Date2024/8/29 20:02 * @Date2024/8/29 20:02
* *
*/ */
@Data
public class Weather { public class Weather {
// private String provinceName;
private String provinceCode;
private String cityName;
private String cityCode;
} }

View File

@ -94,8 +94,8 @@ public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> impleme
*/ */
@Override @Override
public Result getWeather() { public Result getWeather() {
StringBuffer stringBuffer = WeatherInquire.queryWeather(); List<Weather> weathers = WeatherInquire.queryWeather();
return Result.success(stringBuffer); return Result.success(weathers);
} }
/** /**
* *

View File

@ -1,11 +1,17 @@
package com.muyu.cloud.mart.utils; package com.muyu.cloud.mart.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.muyu.domain.News;
import com.muyu.domain.Weather;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -22,27 +28,33 @@ public class WeatherInquire {
public static String API_URL = "https://apis.juhe.cn/fapig/alarm/citys"; public static String API_URL = "https://apis.juhe.cn/fapig/alarm/citys";
// 接口请求Key // 接口请求Key
public static String API_KEY = "80e78ee9adaded8b1f42ec9cbdf69ac9"; public static String API_KEY = "80e78ee9adaded8b1f42ec9cbdf69ac9";
public static StringBuffer queryWeather() { public static List<Weather> queryWeather() {
String apiKey = "80e78ee9adaded8b1f42ec9cbdf69ac9";
String apiUrl = "https://apis.juhe.cn/fapig/alarm/citys";
HashMap<String, String> map = new HashMap<>(); HashMap<String, String> map = new HashMap<>();
map.put("key", API_KEY); map.put("key", API_KEY);
String urlencode = urlencode(map);
List<Weather> list = null;
String s = doGet(API_URL, urlencode);
try { try {
URL url = new URL(String.format(apiUrl + "?" + params(map))); JSONObject jsonObject = JSONObject.parseObject(s);
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream())); int error_code = jsonObject.getInteger("error_code");
String inputLine;
StringBuffer response = new StringBuffer(); if (error_code == 0) {
while ((inputLine = in.readLine()) != null) { System.out.println("调用接口成功");
response.append(inputLine);
JSONObject result = jsonObject.getJSONObject("result");
String data = result.getString("citys");
list = JSON.parseArray(data, Weather.class);
System.out.println(list);
} else {
System.out.println("调用接口失败:" + jsonObject.getString("reason"));
} }
in.close();
System.out.println(response);
return response;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return list;
} }
public static String params(Map<String, String> map) { public static String params(Map<String, String> map) {
return map.entrySet().stream() return map.entrySet().stream()

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff