Flutter get return empty body 但它不是空的

Flutter get return empty body but it is not empty

正如我在标题中所写,我正在尝试从服务器获取数据,但是方法 http.get(Uri.parse(url)) returns 为空 body 和 od 当然是尝试解码空 body 引起的错误, 但是如果我复制粘贴之前使用的 url 我可以看到 json 内容。怎么可能?

我使用的函数是:

Future getData ( ) async {
     String url = 'http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/andamentoTreno/...';
        http.Response response = await http.get(Uri.parse(url));
      
     if (response.statusCode == 204) { 
       var data = response.body; 
       print("data are : $data");
       return jsonDecode(data);
     } else {
       print("unable to load data"); 
     }
 }

这是我应该使用此命令看到的一小部分内容:print("data are : $data");

{
   "tipoTreno":"PG",
   "orientamento":"B",
   "codiceCliente":1,
   "fermateSoppresse":[
      
   ],
   "dataPartenza":null,
   "fermate":[
      {
         "orientamento":"B",
         "kcNumTreno":null,
         "stazione":"VENEZIA SANTA LUCIA",
         "id":"S02593",
         "listaCorrispondenze":null,
         "programmata":1646663160000,
         "programmataZero":null,
         "effettiva":1646663160000,
         "ritardo":0,
         "partenzaTeoricaZero":1646663160000,
         "arrivoTeoricoZero":null,
         "partenza_teorica":1646663160000,
         "arrivo_teorico":null,
         "isNextChanged":false,
         "partenzaReale":1646663160000,
         "arrivoReale":null,
         "ritardoPartenza":0,
         "ritardoArrivo":0,
         "\"pro ...........}"

所以你可以看到 body 是你想要的但不是空的,它是 json 格式。 谁能帮我?我无法解决这个问题。

输出为:

I/flutter (3177): data are:
E/flutter ( 3177): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Unexpected end of input (at character 1)
E/flutter ( 3177): 
E/flutter ( 3177): ^
E/flutter ( 3177): 
E/flutter ( 3177): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1405:5)
E/flutter ( 3177): #1      _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:523:7)
E/flutter ( 3177): #2      _parseJson (dart:convert-patch/convert_patch.dart:41:10)
E/flutter ( 3177): #3      JsonDecoder.convert (dart:convert/json.dart:612:36)
E/flutter ( 3177): #4      JsonCodec.decode (dart:convert/json.dart:216:41)
E/flutter ( 3177): #5      jsonDecode (dart:convert/json.dart:155:10)
E/flutter ( 3177): #6      NetworkHelper.ottieniTutteInfo (package:agenda_ferrovia/utilities/networkConnection.dart:36:14)
E/flutter ( 3177): <asynchronous suspension>
E/flutter ( 3177): #7      _ServizioUIAggiungiTrenoState.build.ottieniOrigine (package:agenda_ferrovia/screens/servizioUI/servizioUI_singolo.dart:110:33)
E/flutter ( 3177): <asynchronous suspension>
E/flutter ( 3177): #8      _ServizioUIAggiungiTrenoState.build.<anonymous closure> (package:agenda_ferrovia/screens/servizioUI/servizioUI_singolo.dart:231:23)
E/flutter ( 3177): <asynchronous suspension>
E/flutter ( 3177): 

感谢大家的支持!我找到问题了! 'url's String' 的某些部分来自另一个调用。 在变量中有“隐藏字符”\n。这就是问题的原因-.- 我已经解决了在保存到用于 'url' 的变量之前删除除字母以外的所有字符的问题。 我希望我的英语足够清楚。

再见!