试图从世界时间 api 颤振发出 json 请求
Posted
技术标签:
【中文标题】试图从世界时间 api 颤振发出 json 请求【英文标题】:trying to make json request from worldtime api flutter 【发布时间】:2021-07-12 12:50:08 【问题描述】:当我尝试从包含 JSON 数据的资产文件夹中获取数据时,我试图通过使用未来构建器从世界时间 API 发出 JSON 请求,但当我尝试从 Internet 获取数据时,它崩溃了
如您所见 这是主要课程
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget
// This widget is the root of your application.
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
class MyHomePage extends StatefulWidget
MyHomePage(Key key, this.title) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
@override
Widget build(BuildContext context)
return Scaffold(
backgroundColor: Colors.green,
body: FutureBuilder(
future:
get('http://api.worldweatheronline.com/premium/v1/weather.ashx?key=65dbd1979bd445e58aa171529203010&q=Europe/London&format=json&num_of_days=1'),
builder: (context, snapshot)
var myData = json.decode(snapshot.data.toString());
String jsonsDataString = myData.body.toString(); // toString of Response's body is assigned to jsonDataString
jsonsDataString = jsonDecode(jsonsDataString);
if (myData == null)
return Center(
child: Text(
'Loading',
style: TextStyle(fontSize: 30, color: Colors.red),
),
);
else
return Center(
child: Text(
myData,
style: TextStyle(fontSize: 30, color: Colors.red),
),
);
));
这是我尝试运行应用程序时的错误
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following FormatException was thrown building FutureBuilder<Response>(dirty, state: _FutureBuilderState<Response>#2a0b7):
Unexpected character (at character 1)
Instance of 'Response'
^
The relevant error-causing widget was:
FutureBuilder<Response> file:///F:/FlutterProjects/learn_json/lib/main.dart:54:15
When the exception was thrown, this was the stack:
#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1394:5)
#1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1261:9)
#2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:926:22)
#3 _parseJson (dart:convert-patch/convert_patch.dart:31:10)
#4 JsonDecoder.convert (dart:convert/json.dart:495:36)
...
【问题讨论】:
你测试你的网址了吗?显示此消息: "data": "error": [ "msg": "API key has been disabled." ]
Postman 使调试和测试 API 调用变得如此简单。对你有很大帮助。
我试过这个网址jsonplaceholder.cypress.io/todos/1 还是一样的错误
【参考方案1】:
您的密钥无效,请使用 Postman 进行检查,您必须 await
回复
【讨论】:
我试过这个网址jsonplaceholder.cypress.io/todos/1 还是一样的错误 您必须等待回复 试试 await get('url'), 没用,我试着做异步,还是没用以上是关于试图从世界时间 api 颤振发出 json 请求的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Flutter 中使用 JSON 正文发出 http DELETE 请求?