天气预警
parent
f8dccc7350
commit
945b9beef0
File diff suppressed because one or more lines are too long
|
@ -1,5 +1,7 @@
|
|||
package com.muyu.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author:chaiyapeng
|
||||
* @Package:com.muyu.domain
|
||||
|
@ -8,5 +10,10 @@ package com.muyu.domain;
|
|||
* @Date:2024/8/29 20:02
|
||||
* 气象预警
|
||||
*/
|
||||
@Data
|
||||
public class Weather {
|
||||
// private String provinceName;
|
||||
private String provinceCode;
|
||||
private String cityName;
|
||||
private String cityCode;
|
||||
}
|
||||
|
|
|
@ -94,8 +94,8 @@ public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> impleme
|
|||
*/
|
||||
@Override
|
||||
public Result getWeather() {
|
||||
StringBuffer stringBuffer = WeatherInquire.queryWeather();
|
||||
return Result.success(stringBuffer);
|
||||
List<Weather> weathers = WeatherInquire.queryWeather();
|
||||
return Result.success(weathers);
|
||||
}
|
||||
/**
|
||||
* 生辰助手
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
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.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -22,27 +28,33 @@ public class WeatherInquire {
|
|||
public static String API_URL = "https://apis.juhe.cn/fapig/alarm/citys";
|
||||
// 接口请求Key
|
||||
public static String API_KEY = "80e78ee9adaded8b1f42ec9cbdf69ac9";
|
||||
public static StringBuffer queryWeather() {
|
||||
String apiKey = "80e78ee9adaded8b1f42ec9cbdf69ac9";
|
||||
String apiUrl = "https://apis.juhe.cn/fapig/alarm/citys";
|
||||
public static List<Weather> queryWeather() {
|
||||
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put("key", API_KEY);
|
||||
|
||||
String urlencode = urlencode(map);
|
||||
List<Weather> list = null;
|
||||
String s = doGet(API_URL, urlencode);
|
||||
try {
|
||||
URL url = new URL(String.format(apiUrl + "?" + params(map)));
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
|
||||
String inputLine;
|
||||
StringBuffer response = new StringBuffer();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
response.append(inputLine);
|
||||
JSONObject jsonObject = JSONObject.parseObject(s);
|
||||
int error_code = jsonObject.getInteger("error_code");
|
||||
|
||||
if (error_code == 0) {
|
||||
System.out.println("调用接口成功");
|
||||
|
||||
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) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
public static String params(Map<String, String> map) {
|
||||
return map.entrySet().stream()
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue