为了账号安全,请及时绑定邮箱和手机立即绑定

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

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 回答

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


0 回复 有任何疑惑可以回复我~
#1

Ai_Cliare

需要修改一下w3d 的方法代码
2023-05-04 回复 有任何疑惑可以回复我~

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

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


0 回复 有任何疑惑可以回复我~

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

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信