Api 使用 get 方法的 Http 请求请求。但不知道 Flutter 中的实际问题
Api Request using Http request for get method. but don't know actual problem in Flutter
我是 flutter 新手,正在学习 api 请求。我请求获取方法。但它显示错误!我收不到任何数据!
在Postman中,运行正常,数据正常!
此api及token仅供测试用!所以,别担心!
Api请求
Future fetchAlbum() async {
final token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjI4OTksImlzcyI6Imh0dHBzOi8vcG9ydGFsLWFwaS5qb21ha2hhdGEuY29tL2FwaS9hdXRoL2xvZ2luIiwiaWF0IjoxNjMxMjUxNjYwLCJleHAiOjE2MzEzMzgwNjAsIm5iZiI6MTYzMTI1MTY2MCwianRpIjoiNlFEUTZCYnBMT0JhdUJoaSJ9.jAY_2nYxjgsIvXZY5vn0vAr_pwF6UBYbSGZ8wqD0YPQ';
final response = await http.get(
Uri.parse('https://portal-api.jomakhata.com/api/getLeaveDetails?token=${token}'),
// Send authorization headers to the backend.
);
final responseJson = jsonDecode(response.body);
if(response.statusCode==200){
print("ok");
print(responseJson);
}
else{
print("error!");
}
return responseJson;
}
控制台出错
D/EGL_emulation(19932): app_time_stats: avg=14700.05ms min=577.55ms max=28822.55ms count=2
E/flutter (19932): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FormatException: Unexpected character (at character 335)
E/flutter (19932): ...ning":14}],"fiscalYear":"2021-2022"}{"message":"SQLSTATE[22001]: String ...
E/flutter (19932): ^
E/flutter (19932):
E/flutter (19932): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
E/flutter (19932): #1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:869:48)
E/flutter (19932): #2 _parseJson (dart:convert-patch/convert_patch.dart:40:10)
E/flutter (19932): #3 JsonDecoder.convert (dart:convert/json.dart:506:36)
E/flutter (19932): #4 JsonCodec.decode (dart:convert/json.dart:157:41)
E/flutter (19932): #5 jsonDecode (dart:convert/json.dart:96:10)
E/flutter (19932): #6 fetchAlbum (package:test_list/main.dart:51:24)
E/flutter (19932): <asynchronous suspension>
E/flutter (19932):
您正在调用的 API 端点存在错误,更准确地说存在 SQL 错误 SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'url' at row 1
。
因此,在 JSON 输出中插入一条错误消息,如下所示:
}{
"message": ...
这不是 Postman 显示的问题,但结果你得到一个无效的 JSON,{
和 }
之间应该有一个 ,
.
所以当你尝试在Flutter中解码它时,你会得到一个错误,因为转换失败:Unhandled Exception: FormatException: Unexpected character
.
我是 flutter 新手,正在学习 api 请求。我请求获取方法。但它显示错误!我收不到任何数据!
在Postman中,运行正常,数据正常!
此api及token仅供测试用!所以,别担心!
Api请求
Future fetchAlbum() async {
final token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjI4OTksImlzcyI6Imh0dHBzOi8vcG9ydGFsLWFwaS5qb21ha2hhdGEuY29tL2FwaS9hdXRoL2xvZ2luIiwiaWF0IjoxNjMxMjUxNjYwLCJleHAiOjE2MzEzMzgwNjAsIm5iZiI6MTYzMTI1MTY2MCwianRpIjoiNlFEUTZCYnBMT0JhdUJoaSJ9.jAY_2nYxjgsIvXZY5vn0vAr_pwF6UBYbSGZ8wqD0YPQ';
final response = await http.get(
Uri.parse('https://portal-api.jomakhata.com/api/getLeaveDetails?token=${token}'),
// Send authorization headers to the backend.
);
final responseJson = jsonDecode(response.body);
if(response.statusCode==200){
print("ok");
print(responseJson);
}
else{
print("error!");
}
return responseJson;
}
控制台出错
D/EGL_emulation(19932): app_time_stats: avg=14700.05ms min=577.55ms max=28822.55ms count=2
E/flutter (19932): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FormatException: Unexpected character (at character 335)
E/flutter (19932): ...ning":14}],"fiscalYear":"2021-2022"}{"message":"SQLSTATE[22001]: String ...
E/flutter (19932): ^
E/flutter (19932):
E/flutter (19932): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
E/flutter (19932): #1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:869:48)
E/flutter (19932): #2 _parseJson (dart:convert-patch/convert_patch.dart:40:10)
E/flutter (19932): #3 JsonDecoder.convert (dart:convert/json.dart:506:36)
E/flutter (19932): #4 JsonCodec.decode (dart:convert/json.dart:157:41)
E/flutter (19932): #5 jsonDecode (dart:convert/json.dart:96:10)
E/flutter (19932): #6 fetchAlbum (package:test_list/main.dart:51:24)
E/flutter (19932): <asynchronous suspension>
E/flutter (19932):
您正在调用的 API 端点存在错误,更准确地说存在 SQL 错误 SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'url' at row 1
。
因此,在 JSON 输出中插入一条错误消息,如下所示:
}{
"message": ...
这不是 Postman 显示的问题,但结果你得到一个无效的 JSON,{
和 }
之间应该有一个 ,
.
所以当你尝试在Flutter中解码它时,你会得到一个错误,因为转换失败:Unhandled Exception: FormatException: Unexpected character
.