html - Flutter 在返回 404 错误的有效 url 上获取请求
问题描述
我正在尝试使用 HackerNews API 创建一个项目,并且我正在发出一个获取请求以尝试使用以下端点获取有关热门故事的 JSON 数据 - https://hacker-news.firebaseio.com/v0/topstories。 json。我正在使用 http dart 包发出获取请求。
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>();
}
解决方案
抱歉,这是我的错误,显然 API 现在要求您在末尾添加一个 print=pretty 标记。
这是有效的端点: https ://hacker-news.firebaseio.com/v0/topstories.json?print=pretty
我觉得奇怪的是,如果没有漂亮的打印标签,代码就无法工作,但现在它正在使用它。
推荐阅读
- html - 为什么我的网页中会显示 p 标签?
- haskell - 如何使用 Haskell 中的理解从嵌套列表中删除空列表
- c - 改变动态 *char 的大小
- excel - 在相同的字符串输入上使用 InStr 查找下一次出现的内容
- xml - xjc 为 IDREF 元素的集合生成不正确的 jaxb 注释
- python - 在 python 中,pop() 选择什么,它是否允许索引?
- javascript - 如何检查我们是否已经使用 ObservableSlim 在 Object 上创建了代理?
- android - 如何将 ViewHolder 中的 ColorStateList 用于 RecyclerView?
- php - 尽管被回显,但我的 json 编码的 PHP 数据并未在 JavaScript 中显示
- c# - 我在 specflow 3 和 nunit 上做错了什么?