首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果我进行API调用,它将返回HTML代码。

如果我进行API调用,它将返回HTML代码。
EN

Stack Overflow用户
提问于 2021-05-14 04:37:06
回答 1查看 1.5K关注 0票数 1

我使用HTTP package来发出API请求,但是当我发出请求时,它会返回Html response

代码语言:javascript
运行
复制
import 'package:clima/services/location.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {
  @override
  void initState() {
    super.initState();
    getLocation();
  }

  void getLocation() async {
    Location location = new Location();
    await location.getCurrentLocation();

    print(location.latitude);
    print(location.longitude);
  }

  void getData() async {
    final url =
        'samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22';
    http.Response response = await http.get(Uri.parse(url));
    print(response.body);
  }

  @override
  Widget build(BuildContext context) {
    getData();
    return Scaffold();
  }
}

在这里,当我打印response body时,它用HTML源代码而不是'JSON‘返回我。

这就是我得到的回应..。

我需要帮助从API获得有效的JSON响应,而不是HTML响应。

编辑:,现在解决了!

EN

Stack Overflow用户

发布于 2021-05-14 05:25:13

您的URL不完整。

更改此代码

代码语言:javascript
运行
复制
void getData() async {
    final url =
        'samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22';
    http.Response response = await http.get(Uri.parse(url));
    print(response.body);
  }

为了这个,

代码语言:javascript
运行
复制
void getData() async {
    final url =
        'https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22';
    http.Response response = await http.get(url));
    print(response.body);
  }

如果成功了就告诉我们。

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67529120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档