dart - 我无法在颤动中对特定的 url 进行 REST api 调用
问题描述
我无法在此 URL 上进行简单的 get 调用 >>>
http://54.254.255.202:8080/user/status/S5454523D/ST.
这个 url 适用于任何其他像 postman、chrome、android 或 react-native 这样的东西。我特别不知道它仅在 20 次尝试中的 1 次中起作用的原因。在日志中我得到这样的东西
E/flutter: [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
Connection closed before full header was received
这也是我用来尝试这个简单的获取请求的代码
Future<dynamic> getStatus() async {
var res;
String url = "http://54.254.255.202:8080/user/status/S5454523D/ST";
print(url);
var response = await http.get(
Uri.encodeFull(url),
headers: {"Accept": "application/json"}
).catchError((error){
print(error);
}).whenComplete((){
print("completed");
});
print(response);
return res;
}
我使用的任何其他 URL 都运行良好。请帮助。
解决方案
Try simplifying your getStatus
method. This version seems to reliably return the decoded json.
Future<Map> getStatus() async {
try {
Response r = await http.get(
'http://54.254.255.202:8080/user/status/S5454523D/ST',
headers: {
'Accept': 'application/json',
},
);
return json.decode(r.body);
} catch (e) {
return {'error': e.toString()};
}
}
main() async {
print(await getStatus());
}
推荐阅读
- node.js - 使用 Google App Engine 或 Google Cloud Compute VM 测试运行我的应用程序?
- swift - 我如何转换为这样的泛型类型:MyType
(ConformingX 是一个符合协议的类) - maven - 你应该在你的 pom 中包含那些已经是你的一些依赖项的依赖项的依赖项吗?
- python - 如何为数据框中的特定类找到使用熊猫的意思?
- arrays - 循环对 numpy 数组中的 i+1 执行操作
- performance - 这个 Haskell Tower of Hanoi 解决方案是更有效还是只是多余的?
- ruby-on-rails - 如何使用 Rails 6 的 alpha 版本?
- php - 在 MVC 项目中使用 Pimple
- matlab - 为什么这不能使用泰勒级数正确评估 e^x?
- apache - Apache:如何防止站点查看者看到我的文件夹?