作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试在 flutter ajax post 中做一个 ajax post
这是我的代码。
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
它失败了,我对 flutter 很陌生,非常感谢帮助。
我打印出了它抛出的异常。
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
无法从中得到任何东西。
当更改为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 版本
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 是旧的
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");
}
这是有效的。
最佳答案
不知道你用的是什么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}");
});
关于dart - 如何让 ajax 进入 flutter 状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49578708/
我是一名优秀的程序员,十分优秀!