如何使用openweathermap api和http软件包获取每小时和16天的天气预报

问题描述 投票:0回答:1

当我在当前天气http请求中替换APIkey和经度和纬度时,打印jsonDecoded天气数据没有问题。但是我以某种方式得到{cod:401,消息:无效的API密钥。}使用它们以相同的lon&lat和APIkey获取每小时和16天天气数据时出错。这是我的代码:

 Future<dynamic> getHourlyWeather() async {
        Location location = Location();
        await location.getCurrentLocation();

        http.Response response = await http.get(
            'https://pro.openweathermap.org/data/2.5/forecast/hourly?lat=${location.latitude}&lon=${location.longitude}&appid=$APIkey');

        if (response.statusCode == 200) {
          String data = response.body;
          return jsonDecode(data);
        } else {
          print(response.statusCode);
        }
      }

    Future<dynamic> get16DaysWeather() async {
        Location location = Location();
        await location.getCurrentLocation();

        http.Response response = await http.get(
            'https://api.openweathermap.org/data/2.5/forecast/daily?lat=${location.latitude}&lon=${location.longitude}&cnt=10&appid=$APIkey');

        if (response.statusCode == 200) {
          String data = response.body;
          return jsonDecode(data);
        } else {
          print(response.statusCode);
        }
      }

 void initState() {
    gethourly();
    super.initState();
  }


  void gethourly() async {
    var hourly = await weather.getHourlyWeather();
    print(hourly);
  }
flutter dart openweathermap forecast
1个回答
0
投票

我发现从openweathermap获取每小时和每天的天气数据需要付费订阅。

© www.soinside.com 2019 - 2024. All rights reserved.