Flutter 'get' 函数不抓取网站内容
Flutter 'get' function does not fetch the content of the website
为了发出网络请求,我使用
import 'package:http/http.dart';
import 'dart:convert';
Response response = await get('http://worldtimeapi.org/api/timezone/Europe/Berlin');
print(response.body);
Map data = jsonDecode(response.body);
response.body
应该return一张地图,
{"abbreviation":"CET","client_ip":"64.185.235.18","datetime":"2020-12-24T13:14:09.198194+01:00","day_of_week":4,"day_of_year":359,"dst":false,"dst_from":null,"dst_offset":0,"dst_until":null,"raw_offset":3600,"timezone":"Europe/Berlin","unixtime":1608812049,"utc_datetime":"2020-12-24T12:14:09.198194+00:00","utc_offset":"+01:00","week_number":52}
但实际上 returns
I/flutter (14782): <!DOCTYPE html>
I/flutter (14782): <html>
I/flutter (14782): <head>
I/flutter (14782): <meta name="viewport" content="width=device-width, initial-scale=1">
I/flutter (14782): <meta charset="utf-8">
I/flutter (14782): <title>Application Error</title>
I/flutter (14782): <style media="screen">
I/flutter (14782): html,body,iframe {
I/flutter (14782): margin: 0;
I/flutter (14782): padding: 0;
I/flutter (14782): }
I/flutter (14782): html,body {
I/flutter (14782): height: 100%;
I/flutter (14782): overflow: hidden;
I/flutter (14782): }
I/flutter (14782): iframe {
I/flutter (14782): width: 100%;
I/flutter (14782): height: 100%;
I/flutter (14782): border: 0;
I/flutter (14782): }
I/flutter (14782): </style>
I/flutter (14782): </head>
I/flutter (14782): <body>
I/flutter (14782): <iframe src="//www.herokucdn.com/error-pages/application-error.html"></iframe>
I/flutter (14782): </body>
I/flutter (14782): </html>
I/flutter (14782): caught error: FormatException: Unexpected character (at character 1)
I/flutter (14782): <!DOCTYPE html>
I/flutter (14782): ^
实际上代码给出了两个结果,但我不知道为什么。对于相同的代码,有时我会得到第一个结果,有时我会得到第二个结果。
ps,我正在关注 YouTube 上的 Net Ninja 教程,我很确定我编写的代码与他基本相同,但我一直收到此错误。
我试图向 API 发出 GET 请求,但它似乎有时会像您提到的那样出错。您尝试调用的 API 可能在内部有一些错误,这不是您的代码的错。 API 托管在 Heroku 上,每当 API 服务器崩溃或发生某些错误时,它们都会发送一个 HTML 错误页面,这就是您收到的内容。我确实注意到它有时会工作,有时会显示错误。所以你的代码可能工作正常,这只是 API 本身的一些问题。
为了发出网络请求,我使用
import 'package:http/http.dart';
import 'dart:convert';
Response response = await get('http://worldtimeapi.org/api/timezone/Europe/Berlin');
print(response.body);
Map data = jsonDecode(response.body);
response.body
应该return一张地图,
{"abbreviation":"CET","client_ip":"64.185.235.18","datetime":"2020-12-24T13:14:09.198194+01:00","day_of_week":4,"day_of_year":359,"dst":false,"dst_from":null,"dst_offset":0,"dst_until":null,"raw_offset":3600,"timezone":"Europe/Berlin","unixtime":1608812049,"utc_datetime":"2020-12-24T12:14:09.198194+00:00","utc_offset":"+01:00","week_number":52}
但实际上 returns
I/flutter (14782): <!DOCTYPE html>
I/flutter (14782): <html>
I/flutter (14782): <head>
I/flutter (14782): <meta name="viewport" content="width=device-width, initial-scale=1">
I/flutter (14782): <meta charset="utf-8">
I/flutter (14782): <title>Application Error</title>
I/flutter (14782): <style media="screen">
I/flutter (14782): html,body,iframe {
I/flutter (14782): margin: 0;
I/flutter (14782): padding: 0;
I/flutter (14782): }
I/flutter (14782): html,body {
I/flutter (14782): height: 100%;
I/flutter (14782): overflow: hidden;
I/flutter (14782): }
I/flutter (14782): iframe {
I/flutter (14782): width: 100%;
I/flutter (14782): height: 100%;
I/flutter (14782): border: 0;
I/flutter (14782): }
I/flutter (14782): </style>
I/flutter (14782): </head>
I/flutter (14782): <body>
I/flutter (14782): <iframe src="//www.herokucdn.com/error-pages/application-error.html"></iframe>
I/flutter (14782): </body>
I/flutter (14782): </html>
I/flutter (14782): caught error: FormatException: Unexpected character (at character 1)
I/flutter (14782): <!DOCTYPE html>
I/flutter (14782): ^
实际上代码给出了两个结果,但我不知道为什么。对于相同的代码,有时我会得到第一个结果,有时我会得到第二个结果。
ps,我正在关注 YouTube 上的 Net Ninja 教程,我很确定我编写的代码与他基本相同,但我一直收到此错误。
我试图向 API 发出 GET 请求,但它似乎有时会像您提到的那样出错。您尝试调用的 API 可能在内部有一些错误,这不是您的代码的错。 API 托管在 Heroku 上,每当 API 服务器崩溃或发生某些错误时,它们都会发送一个 HTML 错误页面,这就是您收到的内容。我确实注意到它有时会工作,有时会显示错误。所以你的代码可能工作正常,这只是 API 本身的一些问题。