如何处理 http 错误(FormatException: Unexpected end of input (at character 1))
Posted
技术标签:
【中文标题】如何处理 http 错误(FormatException: Unexpected end of input (at character 1))【英文标题】:How to handle http errors (FormatException: Unexpected end of input (at character 1)) 【发布时间】:2019-10-08 11:14:22 【问题描述】:如何处理这种错误信息?为什么会来?是那个问题吗?我每 10 秒调用一次下面的方法。
checkQuick(String url, String token) async
result =
(await HelperDatabase1().displayGetUserPreference()).elementAt(0)?.data;
final response = await http.get(
'$url/nativeapi/v1.0/User/GetUserPreference',
headers: 'Authorization': 'Bearer $token',
);
final jsonResponse = json.decode(response.body);
GetUserPreference model = GetUserPreference.fromJson(jsonResponse);
var data = GetUserPreference(data: model.data);
//result = data.data;
if (result != data.data)
// await HelperDatabase1().updateGetUserPreference(1, data.data);
print('inside');
await HelperDatabase1().deleteGetUserPreference();
await HelperDatabase1().storeGetUserPreference(url, token);
有时会出现以下错误消息。
E/flutter ( 7148): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FormatException: Unexpected end of input (at character 1)
E/flutter ( 7148):
E/flutter ( 7148): ^
E/flutter ( 7148):
E/flutter (7148): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1392:5) E/flutter (7148): #1 _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:510:7) E/flutter (7148): #2 _parseJson (dart:convert-patch/convert_patch.dart:30:10) E/flutter (7148): #3 JsonDecoder.convert (dart:convert/json.dart:493:36) E/flutter (7148):
4 JsonCodec.decode (dart:convert/json.dart:151:41) E/flutter (7148): #5 _ListPageState.checkQuick
(package:reborn_next_job02/ui/AssetRegisters.dart:153:31) E/flutter ( 7148):E/颤振(7148):#6 _ListPageState.initState。 (包:reborn_next_job02/ui/AssetRegisters.dart:47:7) E/flutter ( 7148): #7 _rootRunUnary (dart:async/zone.dart:1132:38) E/flutter (7148):#8 _CustomZone.runUnary (dart:async/zone.dart:1029:19) E/颤振(7148):#9 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7) E/flutter (7148): #10 _CustomZone.bindUnaryCallbackGuarded。 (dart:async/zone.dart:968:26) E/flutter (7148): #11 _rootRunUnary (dart:async/zone.dart:1136:13) E/flutter (7148): #12 _CustomZone.runUnary (dart:async/zone.dart:1029:19) E/flutter (7148): #13 _CustomZone.bindUnaryCallback。 (dart:async/zone.dart:952:26) E/flutter (7148): #14 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19) E/flutter (7148): #15 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5) E/flutter (7148): #16 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
【问题讨论】:
【参考方案1】:检查之前的 response.statusCode 是否为 200。 貌似response.body是空的,因为调用失败,所以json.decode(response.body)抛出异常。
【讨论】:
以上是关于如何处理 http 错误(FormatException: Unexpected end of input (at character 1))的主要内容,如果未能解决你的问题,请参考以下文章
如何处理 Parse.com 将 HTTP 错误转换为 NSError?