如何处理http错误(FormatException:输入的意外结束(在字符1处))

时间:2019-05-22 03:12:23

标签: dart flutter flutter-dependencies

如何处理这种错误消息?为什么会来?那是问题吗?我每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 / flutter(7148):#6
  _ListPageState.initState。 (package: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 / flutter(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)

2 个答案:

答案 0 :(得分:0)

请先检查response.statusCode是否为200。 由于调用失败,response.body似乎为空,因此json.decode(response.body)引发异常。

答案 1 :(得分:-1)

我认为这里的问题是因为您不是以JSON格式获取HTTP数据,因此无论何时尝试解码,它都会爆炸,因为它将找到非JSON格式的代码

我应该尝试将$url称为.json,例如:

final response = await http.get(
  '$url/nativeapi/v1.0/User/GetUserPreference.json',
  headers: {'Authorization': 'Bearer $token'},
);

也许我错了,但是尝试一下。