Flutter:json.decode FormatException(FormatException:输入的意外结束(在字符1处)

时间:2019-08-17 00:21:57

标签: http flutter dart

我想使用rest API从服务器中获取一些数据。这段代码一切正常:

函数获取我

Future<List<Mosque>> getMahasiswa() async {
        var apiRespon = await http.get('$baseURL/mahasiswa/');
        if(apiRespon.statusCode == 200 ){
          final apiResponJson = json.decode(apiRespon.body);
          return (apiResponJson['data'] as List).map((p)=>Mosque.fromJson(p)).toList();
        }else{
          throw Exception('Failed Load');
        }
  }

但是当我将其更改为时出现错误:

它将给我以下内容

Exception has occurred.
FormatException (FormatException: Unexpected end of input (at character 1)

^
)

函数Get II

Future getMahasiswaRevisi() async{
    var rq = http.Request('GET',Uri.parse('$baseURL/mahasiswa/'));
    http.StreamedResponse response = await http.Client().send(rq);
    int statusCode = response.statusCode;
    final responseJson = json.decode(rq.body); <- THIS LINE ERROR APPEAR
    statusCode == 200 ? print('success get mahasiswa with statusCode $statusCode $responseJson ') : print('failed with statusCode : $statusCode') ;
  }

如果您想知道为什么即使功能获取我仍然可以更改代码,那是因为我想将所有功能 CRUD Operation 更改为一样,就像这样:

  Future addMahasiswaRevisi(headers,body) async{
    http.Request rq = http.Request('POST',Uri.parse('$baseURL/mahasiswa/add/'))..headers.addAll(headers);
    rq.bodyFields=body;
    http.StreamedResponse response = await http.Client().send(rq);
    int statusCode = response.statusCode;
    print(statusCode.toString());
    print(rq.toString());
    statusCode == 201 ? print('Successs Add with status Code $statusCode') : print('Fail with StatusCode : $statusCode');
  }

  Future deleteMahasiswaRevisi(headers,body) async{
    http.Request rq = http.Request('DELETE',Uri.parse('$baseURL/mahasiswa/delete/'))..headers.addAll(headers);
    rq.bodyFields=body;
    http.StreamedResponse response =await http.Client().send(rq);
    int statusCode = response.statusCode;
    print(statusCode.toString());
    print(rq.toString());
    statusCode == 204 ? print('Successs Delete with status Code $statusCode') : print('Fail with StatusCode : $statusCode');
  }

  Future updateMahasiswaRevisi(dynamic headers, dynamic body) async{
     http.Request rq = http.Request('PUT',Uri.parse('$baseURL/mahasiswa/update/'))..headers.addAll(headers);
     rq.bodyFields=body;
     http.StreamedResponse response = await http.Client().send(rq);
     int statusCode = response.statusCode;
     print(statusCode.toString());
     print(rq.toString());
     statusCode == 204 ? print('Success Update with Status Code $statusCode') : print('Fail With StatusCode : $statusCode');
  }

我对此代码进行了一些比较,并打印了以下内容

var apiRespon = await http.get('$baseURL/mahasiswa');
var rq = http.Request('GET',Uri.parse('$baseURL/mahasiswa/'));
print(apiRespon.body);
print(rq.body);

产生的结果:

print(apiRespon.body);
->{"status":true,"data":[{"id":"127","nrp":"4444","nama":"Agus Setiawan","email":"agus.setiawan@gmail.com","jurusan":"Teknik Komputer"},{"id":"128","nrp":"9090","nama":"Zeffry Reynando","email":"zeffry.reynando@gmail.com","jurusan":"Rekayasa Perangkat Lunak"}]}
print(rq.body);
-> Null {}

我不知道为什么会出错,我尝试打印($ responseJson),但应用程序立即崩溃。

希望大家都能帮助我。
谢谢

0 个答案:

没有答案
相关问题