首页 文章

怎么做ajax得到扑腾?

提问于
浏览
0

我试图在颤抖的ajax帖子做一个ajax帖子

这是我的代码 .

get() async {
      var url = 'https://jsonplaceholder.typicode.com/users';
      var httpClient = new HttpClient();

      String result;
      try {
        var request = await httpClient.getUrl(Uri.parse(url));
        var response = await request.close();
        if (response.statusCode == HttpStatus.OK) {
          var jsonString = await response.transform(utf8.decoder).join();
          var data = json.decode(jsonString);
          result = data;
        } else {
          result =
              'Error getting IP address:\nHttp status ${response.statusCode}';
        }
      } catch (exception) {
        result = 'Failed getting IP address';
      }

      print(result);
    }

当我尝试 https://httpbin.org/ip 时它工作正常 . 但是由于 https://jsonplaceholder.typicode.com/users 它失败了,我很新闻,并且非常感激 .

我打印出它抛出的异常 .

type'_BoundSinkStream <dynamic,List <int >>'不是类型的子类型
'_HttpIncoming'在哪里
_BoundSinkStream来自dart:async List来自dart:core int来自dart:core List来自dart:core int来自dart:core
_HttpIncoming来自dart:_http List来自dart:core int来自dart:core

无法做出任何改变 .

更改为 String result to List result = []

type '_BoundSinkStream<dynamic, List<int>>' is not a subtype of type '_HttpIncoming' where
_BoundSinkStream is from dart:async
List is from dart:core
int is from dart:core
List is from dart:core
int is from dart:core
_HttpIncoming is from dart:_http
List is from dart:core
int is from dart:core

使用更多调试信息更新了代码:

getData() async {
    var url = 'https://jsonplaceholder.typicode.com/users';
    var httpClient = new HttpClient();

    List result = [];
    try {
      print('0');
      var request = await httpClient.getUrl(Uri.parse(url));
      print('1');
      var response = await request.close();
      print('2');
      if (response.statusCode == HttpStatus.OK) {
        print('3');
        var jsonString = await response.transform(utf8.decoder).join();
        print('4');
        var data = json.decode(jsonString);
        print('5');
        result = data;
        print('6');
      } else {
        // result = 'Error getting IP address:\nHttp status ${response.statusCode}';
      }
    } catch (exception) {
      // result = 'Failed getting IP address';
      print('7');
      print(exception);
      print('8');
    }

    print(result);
  }

例外:

0
1
2
3
7
type '_BoundSinkStream<dynamic, List<int>>' is not a subtype of type '_HttpIncoming' where
_BoundSinkStream is from dart:async
List is from dart:core
int is from dart:core
List is from dart:core
int is from dart:core
_HttpIncoming is from dart:_http
List is from dart:core
int is from dart:core
8
[]

颤动版

Flutter 0.1.5 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 3ea4d06340 (5 weeks ago) • 2018-02-22 11:12:39 -0800
Engine • revision ead227f118
Tools • Dart 2.0.0-dev.28.0.flutter-0b4f01f759

真的很老我更新了扑腾

Flutter 0.2.3 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 5a58b36e36 (3 weeks ago) • 2018-03-13 13:20:13 -0700
Engine • revision e61bb9ac3a
Tools • Dart 2.0.0-dev.35.flutter-290c576264

现在我有了一个新错误 .

0
1
2
3
4
5
7
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List' where
_InternalLinkedHashMap is from dart:collection
String is from dart:core
List is from dart:core
8

我更新了GitHub帖子中的代码

getData() async {
    print("0");
    var response = await http.get(
        Uri.encodeFull("https://jsonplaceholder.typicode.com/users"),
        headers: {"Accept": "application/json"});
    print("1");
    data = json.decode(response.body);
    print("2");
    print(data[1]["name"]);
    print("3");
    // print(response.body);
    print("end");
  }

这是有效的 .

2 回答

  • 1

    我不知道你正在使用什么http包 .

    你可以用这个http package

    它使用以下代码与两个url一起使用

    var url = 'https://jsonplaceholder.typicode.com/users';
    //var url = "https://httpbin.org/ip";
    http.get(url)
        .then((response) {
      print("Response status: ${response.statusCode}");
      print("Response body: ${response.body}");
    });
    
  • 2

    要解码JSON数组,您需要一个字符串列表,尝试更改

    String result;
    

    List result = [];
    

相关问题