respnse.HTTP包的正文没有返回所需的内容



我正在构建一个应用程序,该应用程序根据给定的坐标(纬度和经度)返回有关城市的天气信息。只要我打印'print(response .body'),我就会得到一个html代码,而不是所需的天气信息。这是我的代码

Main.dart

import 'package:flutter/material.dart';
import 'package:clima/screens/loading_screen.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: LoadingScreen(),
);
}
}
Location_Screen.dart
import 'package:clima/location.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:http/http.dart' as http;
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
double MyMarginAsDouble;
class _LoadingScreenState extends State<LoadingScreen> {
void initState(){//if i call getlocation inside init maethod it automatically gets the location without the need of a button
super.initState();
getLocation();
}
Future<void> getLocation()
{
location ob= location();
ob.getCurrentLocation();
}
Future<void> getData()
async {
var url = Uri.parse('api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=92e463e61258ef5a8b9a9bd11733b62c');
var response = await http.get(url);
//code which tells different thing 200 - success 404-failure
if(response.statusCode==200)
{
String data = response.body;
print(data);
}
else
{
print(response.statusCode);
}
}
@override
Widget build(BuildContext context) {
getData();
return Scaffold();
}
}

Location.dart
import 'package:geolocator/geolocator.dart';
class location{
double latitude;
double longitude;
Future<void> getCurrentLocation()
async {
try{
LocationPermission permission = await Geolocator.requestPermission();
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low); //high is battery consuming
LocationPermission permission1 = await Geolocator.checkPermission();
latitude=position.latitude;
longitude=position.longitude;
print(longitude);
print(latitude);
}
catch(e)
{
print(e);
}
}
}

这是我得到的输出

<html>
<head>
<base href="/">
</head>
<body>
<script src="main.dart.js"></script>
</body>
</html>
77.5578876
12.9219832

我无法访问伦敦市的天气数据

(我想添加这个作为评论,但我没有足够的代表发表评论)

查看您的代码:Uri。Parse采用一个模式,如https://或http://, so:

var url = Uri.parse('http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=92e463e61258ef5a8b9a9bd11733b62c');

查看Url源,我不知道没有方案它会做什么,但如果没有方案,它似乎不会抛出错误。尝试添加一个方案(http://)),看看是否得到正确的资源。

相关内容

  • 没有找到相关文章

最新更新