王小east
2022-01-19 23:54
大佬们,这个报错原因是啥呀
public List<DayWeather> w3d(String appCode, String area) { List<DayWeather> resultList = new ArrayList<DayWeather>(); try { //新建查询请求 OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .get() .url("http://ali-weather.showapi.com/day15?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>> dayList = this.getValue(result, "showapi_res_body.dayList", ArrayList.class); //如果没有找到对应地区天气数据,返回空的List列表 if (dayList == null || dayList.size() == 0) { return new ArrayList<DayWeather>(); } Iterator<Map<String, String>> iterator = dayList.iterator(); for (int i = 0; i < 3; i++) { if (iterator.hasNext()) { Map<String, String> day = iterator.next(); DayWeather dayWeather = new DayWeather(); dayWeather.setYear(day.get("daytime").substring(0, 4)); dayWeather.setMonth(day.get("daytime").substring(4, 6)); dayWeather.setDay(day.get("daytime").substring(6, 8)); dayWeather.setDayAirTemperature(day.get("day_air_temperature")); dayWeather.setNightAirTemperature(day.get("night_air_temperature")); dayWeather.setDayWeather(day.get("day_weather")); dayWeather.setNightWeather(day.get("night_weather")); dayWeather.setDayWindPower(day.get("day_wind_power")); dayWeather.setNightWindPower(day.get("night_wind_power")); resultList.add(dayWeather); } } // ************* 以上是修改部分 ******************** } catch (Exception e) { throw new RuntimeException(e); } return resultList; }
你下的那个阿里云里面的天气预报的版本不一样,要下跟老师一样的那个版本,就是有免费版三个字的版本
搜索
复制
Java入门第二季 升级版
530560 学习 · 6091 问题
相似问题