【问题标题】:Flutter get request on a valid url returning 404 ErrorFlutter 在返回 404 错误的有效 url 上获取请求
【发布时间】:2021-03-02 06:19:49
【问题描述】:

我正在尝试使用 HackerNews API 创建一个项目,并且我正在发出一个获取请求以尝试使用以下端点 - 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

get 请求运行,但是当我尝试解码响应对象正文中的 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>();
  }

【问题讨论】:

  • 这里面的客户是什么??
  • 我将编辑问题并包含客户信息
  • 最终响应 = 等待 http.get(url);尝试替换此行并检查它
  • 我试过了,没有改错。

标签: html json flutter dart get


【解决方案1】:

我在邮递员检查端点时得到了这种类型的响应

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 嘿,我从hackernews 文档-github.com/HackerNews/API 获得了我的API 端点,在我的浏览器上跟踪API 端点似乎显示正确的信息。
      • @Akashvshroff 现在请检查我的帖子我现在已经编辑了我的帖子。请让我知道它是否正在运行。
      • 嘿,这正是我一直在使用的 API url。
      猜你喜欢
      • 1970-01-01
      • 2011-10-24
      • 2022-11-04
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 2018-05-06
      相关资源
      最近更新 更多