gpt4 book ai didi

json - 从网络服务中获取 json 数组

转载 作者:IT王子 更新时间:2023-10-29 07:23:31 25 4
gpt4 key购买 nike

我正在尝试从网络服务中获取一个 json 数组。

文档说:

_getIPAddress() {
final url = 'https://httpbin.org/ip';
HttpRequest.request(url).then((value) {
print(json.decode(value.responseText)['origin']);
}).catchError((error) => print(error));
}

如果我使用这段代码,我会得到错误:

The method request is not defined for the class 'HttpRequest'

如果我尝试导入:

import 'dart:html';

我收到这个错误:

Target of URI doesn't exist 'dart:html'

最佳答案

对于 http 请求,我推荐 http package .

然后在导入 http 包之后,你可以像这样使用它,例如:

import 'package:http/http.dart' as http;

_getIPAddress() async {
final url = 'https://httpbin.org/ip';
try {
http.Response res = await http.get(url);
print(json.decode(res.body));
} catch(e) {
print(e);
}
}

关于json - 从网络服务中获取 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54289420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com