24小时查的到,但是3天七天却查不到信息,这是为啥,明明是同样的代码

来源:5-5 实现未来7天天气预报查询功能

weixin_慕丝1473147

2021-03-23 19:46

if(a==1){

System.out.print("请输入城市名称查询24小时天气预报:");

String city=in.next();

    WeatherUtils weatherUtils =new WeatherUtilsImpl();

    List<HourWeather> weatherList=weatherUtils.w24h("0d8abeeafc364b5aaa73b7e52ceb83ba", city);

//     System.out.println(weatherList);

    if(weatherList.size()==0){

    System.out.println("未查询到数据");

    }else{

    for(HourWeather hourWeather:weatherList){

    String template="%s月%s日%s时|%-3s|%-20s|%-8s|%-4s";

String row=String.format(template, new String[]{

    hourWeather.getMonth(),

    hourWeather.getDay(),

    hourWeather.getHour(),

    hourWeather.getWindDirection(),

    hourWeather.getWindPower(),

    hourWeather.getWeather(),

    hourWeather.getTemperature()

    });

System.out.println(row);

    }

    }

}else if(a==2){

System.out.print("请输入城市名称查询3天天气预报:");

String city=in.next();

WeatherUtils weatherUtils =new WeatherUtilsImpl();

    List<DayWeather> weatherList=weatherUtils.w3d("0d8abeeafc364b5aaa73b7e52ceb83ba", city);

//     System.out.println(weatherList);

    if(weatherList.size()==0){

    System.out.println("未查询到信息");

    }else{

    for(DayWeather weather:weatherList){

    String template="%-2s月%-2s日|气温:%s(日)%s(夜)|天气:%s(日)%s(夜)|风力:%s(日)%s(夜)";

    String row =String.format(template, new String[]{

    weather.getMonth(),

    weather.getDay(),

    weather.getDayAirTemperature(),

    weather.getNightAirTemperature(),

    weather.getDayWeather(),

    weather.getNightWeather(),

    weather.getDayWindPower(),

    weather.getNightWindPower()

    });

    System.out.println(row);

    }

    }

    

}else if(a==3){

System.out.print("请输入城市名称查询7天天气预报:");

String city=in.next();

WeatherUtils weatherUtils =new WeatherUtilsImpl();

List<DayWeather> weatherList=weatherUtils.w7d("0d8abeeafc364b5aaa73b7e52ceb83ba", city);

//     System.out.println(weatherList);

    if(weatherList.size()==0){

    System.out.println("未查询到信息");

    }else{

    for(DayWeather weather:weatherList){

    String template="%-2s月%-2s日|气温:%s(日)%s(夜)|天气:%s(日)%s(夜)|风力:%s(日)%s(夜)";

    String row =String.format(template, new String[]{

    weather.getMonth(),

    weather.getDay(),

    weather.getDayAirTemperature(),

    weather.getNightAirTemperature(),

    weather.getDayWeather(),

    weather.getNightWeather(),

    weather.getDayWindPower(),

    weather.getNightWindPower()

    });

    System.out.println(row);

    }

    }

    

}


写回答 关注

3回答

  • Ai_Cliare
    2023-05-04 16:32:45
    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;
    }


    Ai_Cli...

    需要修改一下w3d 的方法代码

    2023-05-04 16:33:19

    共 1 条回复 >

  • 慕沐8789467
    2022-08-25 20:08:08

    已经 按老师的教程完成啦,供大家参考

    https://github.com/carrieguo/java-weather-forecast.git


  • 帝国大学
    2021-05-05 09:44:50

    可能是该城市没有3天7天的数据,可以直接到API页面查询验证数据是否存在。

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530696 学习 · 6091 问题

查看课程

相似问题