建议更新jar包
老师啊,能不能把url里面的访问网址也改成参数传入后更新个jar包呀?这个API的访问参数有变化了:

老师啊,能不能把url里面的访问网址也改成参数传入后更新个jar包呀?这个API的访问参数有变化了:

2021-01-24
可以新创建一个temp_WeatherUtilsImpl.java文件,来重写WeatherUtilsImpl类,此处仅以24h查询为例:
package com.imooc.weather;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.imooc.weather.impl.WeatherUtilsImpl;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class temp_WeatherUtilsImpl extends WeatherUtilsImpl {
//重载或重写WeatherUtilsImpl.java
private <T> T getValue(Object map, String path, Class<T> clazz) {
try {
OgnlContext context = new OgnlContext();
context.setRoot(map);
T value = (T) Ognl.getValue(path, context, context.getRoot());
return value;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public List<HourWeather> w24h(String appCode, String area) {
List<HourWeather> resultList = new ArrayList<HourWeather>();
try {
//新建查询请求
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.get()
.url("https://ali-weather.showapi.com/hour24?area=" + area)
.header("Authorization", "APPCODE " + appCode)
.build();
Call call = client.newCall(request);
Response response = call.execute();
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
//获取服务器返回数据
String resBody = response.body().string();
Map result = gson.fromJson(resBody, new TypeToken<Map>() {
}.getType());
//包装为对象集合
List<Map<String, String>> hourList = this.getValue(result, "showapi_res_body.hourList", ArrayList.class);
//如果没有找到对应地区天气数据,返回空的List列表
if(hourList == null || hourList.size() == 0){
return new ArrayList<HourWeather>();
}
for (Map<String, String> hour : hourList) {
HourWeather hourWeather = new HourWeather();
hourWeather.setYear(hour.get("time").substring(0, 4));
hourWeather.setMonth(hour.get("time").substring(4, 6));
hourWeather.setDay(hour.get("time").substring(6, 8));
hourWeather.setHour(hour.get("time").substring(8, 10));
hourWeather.setWindDirection(hour.get("wind_direction"));
hourWeather.setWindPower(hour.get("wind_power"));
hourWeather.setWeather(hour.get("weather"));
hourWeather.setTemperature(hour.get("temperature"));
resultList.add(hourWeather);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return resultList;
}
public static void main(String[] args) throws IOException, OgnlException {
List<DayWeather> list2 = new WeatherUtilsImpl().w3d("28a8e0cdea484fdd9cba138a72bc1778", "DASDSADSA");
System.out.println(list2);
}
}
这是Application.java:
package com.imooc.weather;
import com.imooc.weather.impl.WeatherUtilsImpl;
import java.util.List;
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
System.out.println("查询最近天气预报");
System.out.println("输入1: 查询未来24小时天气预报");
System.out.println("输入2: 查询未来3天天气预报");
System.out.println("输入3: 查询未来7天天气预报");
System.out.print("请输入您的选择: ");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
System.out.println("用户输入数字: " + i);
if(i == 1){
System.out.print("请输入城市名称查询未来24小时天气预报: ");
String city = scanner.next();
temp_WeatherUtilsImpl weatherUtils = new temp_WeatherUtilsImpl();
List<HourWeather> weatherList1 = weatherUtils.w24h("5f94543d72c44591bf4897e0f3d622ea", city);
System.out.println(weatherList1);
}
}
}举报