【问题标题】:How to get Hourly and 16-day weather forecast using openweathermap api and http package如何使用 openweathermap api 和 http 包获取每小时和 16 天的天气预报
【发布时间】:2020-05-12 02:09:55
【问题描述】:

当我在当前天气 http 请求中替换 APIkey 和经度和纬度时,打印 jsonDecoded 天气数据没有问题。但我以某种方式得到 {cod: 401, message: Invalid API key.} 错误,使用它们获取具有相同 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);
  }

【问题讨论】:

  • 一个api url是https://pro....,另一个是https://api....可以吗?
  • 您可能需要重新检查您的密钥以及它在 OpenWeather 上的访问权限。
  • 是的,在网站上它说 (pro. ...) 每小时一次,但我也尝试了 api 和 pro
  • 我已经检查了很多,我将密钥作为常量,当我使用它来获取当前天气时它可以工作,但是对于每小时和每天的天气它不起作用

标签: flutter dart openweathermap forecast


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    可以通过使用onecall端点来完成,这个端点提供每小时和每天的天气:

    https://api.openweathermap.org/data/2.5/onecall?&amp;units=metric&amp;exclude=minutely&amp;appid=${appKey}&amp;lat=${cityData.coord.lat}&amp;lon=${cityData.coord.lon};

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-01
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多