【发布时间】: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