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

标签 json flutter httprequest fetch

我正在尝试从网络服务中获取一个 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/

相关文章:

ruby-on-rails - 具有强参数的 Rails 4 API?

javascript - 访问 JSON 整数元素

javascript - 从 Osmosis 函数内部将 json 对象写入 csv

json - 粘贴缺少的VS2019的特殊内容(JSON到类)-安装了所有Web开发工具

ios - 如何监控来自WKWebView的http请求?

android - 自定义画家在 ListView 图 block 更改时不会更改

android - 在 android studio 中如何从磁盘重新加载代码文件?

android - "The method ' openDrawer ' was called on null."我需要一些关于 Flutter 的帮助

android - 在异步 Http 客户端上设置超时

java - 如何在java中处理阿拉伯语