使用flutter http包获取天气信息



我想从openweathermap API获取协调基地的当前天气信息。为此,我使用flutter的HTTP包。

当我使用这种方法时,问题就在这里

还有一个问题是。。。我在Memu App Player上得到了这些坐标,但在Real android设备和android Studio自己的Emulator上没有给出

void getWeatherData()async{
String url = 'http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=331533c0cc2197e929ea79cdb2a70e33';
Response response = await get(Uri.parse(url));
print(response.body);  }

这应该会完全给我API结果,但当我运行我的应用程序时,它会给我这个错误。

Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform: http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=331533c0cc2197e929ea79cdb2a70e33

我就是这样做的。我正在乌代米学习一门课程,这门课程正在复习你的要求。

void getData() async {
var url = Uri.https('samples.openweathermap.org', 'data/2.5/weather', {
'lat': '35',
'lon': '139',
'appid': 'b6907d289e10d714a6e88b30761fae22'
});
http.Response response = await http.get(url);
print(weatherResponse.body);
}

记住使用

import 'package:http/http.dart' as http;

最新更新