无法从 Flutter 中的开放天气 API 获取数据
Unable to Fetch Data from Open Weather API in Flutter
我的结果
源代码
加载屏幕
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:clima/services/location.dart';
import 'package:http/http.dart'as http;
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
void getlocation()async{
Location location=Location();
await location.getcurrentlocation();
print(location.lat);
print(location.long);
}
void getdata()async{
http.Response response = await http.get(Uri.parse
('samples.openweathermap.org/data/2.5/forecast?q=London,us&mode=xml&appid=b6907d289e10d714a6e88b30761fae22')
);
if (response.statusCode == 200) {
String data = response.body;
print(data);
} else {
print(response.statusCode);
}}
Widget build(BuildContext context) {
getlocation();
getdata();
return Scaffold(
);
}
}
获取当前位置方法
import 'package:geolocator/geolocator.dart';
class Location{
double long;
double lat;
Future<void> getcurrentlocation() async {
try {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
lat = position.latitude;
long= position.longitude;
} catch (e) {
}
}
}
我想要这个结果(https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22)但是无法得到这个。你能告诉我哪里错了吗
您必须将 https://
添加到您的 url。
像这样:
Uri.parse('https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22');
此外,请始终post您的代码是文本格式,而不是图像格式。
我的结果
源代码
加载屏幕
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:clima/services/location.dart';
import 'package:http/http.dart'as http;
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
void getlocation()async{
Location location=Location();
await location.getcurrentlocation();
print(location.lat);
print(location.long);
}
void getdata()async{
http.Response response = await http.get(Uri.parse
('samples.openweathermap.org/data/2.5/forecast?q=London,us&mode=xml&appid=b6907d289e10d714a6e88b30761fae22')
);
if (response.statusCode == 200) {
String data = response.body;
print(data);
} else {
print(response.statusCode);
}}
Widget build(BuildContext context) {
getlocation();
getdata();
return Scaffold(
);
}
}
获取当前位置方法
import 'package:geolocator/geolocator.dart';
class Location{
double long;
double lat;
Future<void> getcurrentlocation() async {
try {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
lat = position.latitude;
long= position.longitude;
} catch (e) {
}
}
}
我想要这个结果(https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22)但是无法得到这个。你能告诉我哪里错了吗
您必须将 https://
添加到您的 url。
像这样:
Uri.parse('https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22');
此外,请始终post您的代码是文本格式,而不是图像格式。