微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter 在有效 url 上获取请求,返回 404 错误

如何解决Flutter 在有效 url 上获取请求,返回 404 错误

我正在尝试使用 HackerNews API 创建一个项目,并且我正在发出一个 get 请求,以尝试使用以下端点 - https://hacker-news.firebaseio.com/v0/topstories.json 获取有关头条新闻的 JSON 数据。我正在使用 http dart 包发出 get 请求。

pubspec.yaml 文件

dependencies:
  Flutter:
    sdk: Flutter
  sqflite: ^1.3.0
  path_provider: ^1.6.27
  http: ^0.12.2
  path: ^1.7.0
  intl: ^0.16.1
  rxdart: ^0.25.0

获取请求运行但是当我尝试解码响应对象正文中的 JSON 信息时,我收到以下错误

Exception has occurred.
FormatException (FormatException: Unexpected character (at character 1)
<!DOCTYPE html>
^
)

我的代码如下:

 Future<List<int>> fetchTopIds() async {
    //root is the base URL as is 'http://hacker-news.firebaseio.com/v0'
    final String url = '$root/topstories.json'; 
    print(url); //prints out the valid API endpoint
    final response = await client.get(url);
    print(response.body); //prints out some HTML,see below
    final ids = json.decode(response.body); //error
    return ids.cast<int>();
  }

通过打印 response.body 显示的 HTML:

I/Flutter ( 7425): <!DOCTYPE html>
I/Flutter ( 7425): <html lang=en>
I/Flutter ( 7425):   <Meta charset=utf-8>
I/Flutter ( 7425):   <Meta name=viewport content="initial-scale=1,minimum-scale=1,width=device-width">
I/Flutter ( 7425):   <title>Error 404 (Not Found)!!1</title>
I/Flutter ( 7425):   <style>
I/Flutter ( 7425):     *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/brand

我该如何解决并修复此错误?谢谢

编辑: 客户端是来自 HTTP 包的 Client 类的实例,是该类的实例变量。

class NewsApiProvider implements Source {
  final Client client = Client();

  Future<List<int>> fetchTopIds() async {
    final String url = '$root/topstories.json';
    print(url);
    final response = await client.get(url);
    print(response.body);
    final ids = jsonDecode(response.body);
    return ids.cast<int>();
  }

解决方法

请验证您的数据您已传递哪些数据。因为我已经在邮递员那里检查了你的参数。你传递了错误的参数。

您传递了错误的网址。请使用此https://hacker-news.firebaseio.com/v0/topstories.json

enter image description here

,

我在邮递员那里得到了这种类型的响应,检查端点是否正确

enter image description here

,

抱歉,这是我的错误,显然 API 现在要求您在末尾添加一个 print=pretty 标签。

这是有效的端点: https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty

我觉得奇怪的是,如果没有漂亮的打印标签,代码就无法运行,但现在可以使用了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。