问答详情
源自:5-4 实现未来3天天气预报查询功能

大佬们,这个报错原因是啥呀

大佬们,这个报错原因是啥呀http://img1.mukewang.com/61e834010001fdf028001608.jpg

提问者:王小east 2022-01-19 23:54

个回答

  • Ai_Cliare
    2023-05-04 16:34:33

    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;
    }


  • 猿多多
    2022-08-11 10:16:24

    你下的那个阿里云里面的天气预报的版本不一样,要下跟老师一样的那个版本,就是有免费版三个字的版本

    搜索

    复制