几天来一直在尝试解决此问题。该代码成功地为我提供了JSON数据,但是缺少很多块,使我无法将其解析为对象。代码和输出如下。研究了很多,欢迎提出任何建议。
class TBAState extends State<TBAData>{
String url = "http://www.thebluealliance.com/api/v3/status";
HttpClient myhttp = new HttpClient();
List data;
List cellTitle;
Future getSWData() async {
myhttp.getUrl(Uri.parse(url)).then((HttpClientRequest request) {
request.headers.set("accept", "application/json");
request.headers.set("X-TBA-Auth-Key", "XXXXX");
return request.close();
}).then((HttpClientResponse response) {
response.transform(utf8.decoder).listen((contents) {
// print(contents);
});
});
myhttp.get('www.thebluealliance.com', 80, '/api/v3/teams/0/simple')
.then((HttpClientRequest request){
request.headers.set("accept", "application/json");
request.headers.set("X-TBA-Auth-Key", "XXXX");
return request.close();
})
.then((HttpClientResponse response) {
response.transform(utf8.decoder).listen((content) {
//buffer.writeAll(content.toString());
print(content);
});
});
}
}
某些输出:
“ team_number”:6 }
{<...> <-截断
颤振:布鲁克曼和帕克维尔高中及数学中心,
科学与计算机科学”,
“昵称”:“ Team007”,
“ state_prov”:“ MD”,
“ team_number”:7
},
{
Error:
Dart Error: Unhandled exception:
FormatException: Unterminated string (at line 42, character 155)
...y/US Army Aberdeen Test Center/Moffatt & Nichol/Baltimore Area Alliance/Mr.
^
#0 _ChunkedJsonParser.fail (dart:convert/runtime/libconvert_patch.dart:1358:5)
#1 _ChunkedJsonParser.close (dart:convert/runtime/libconvert_patch.dart:504:9)
#2 _parseJson (dart:convert/runtime/libconvert_patch.dart:30:10)
#3 JsonDecoder.convert (dart:convert/json.dart:540:36)
#4 JsonCodec.decode (dart:convert/json.dart:167:41)
#5 TBAState.getSWData
答案 0 :(得分:0)
已修复:问题出在HttpClientResonse的转换中。正确的代码:
class TBAState extends State<TBAData>{
HttpClient myhttp = new HttpClient();
List<dynamic> data;
List cellTitle;
List<Team> teamData;
Future getSWData() async {
myhttp.get('www.thebluealliance.com', 80, '/api/v3/status').then((HttpClientRequest request) {
request.headers.set("accept", "application/json");
request.headers.set("X-TBA-Auth-Key", "XXXX");
return request.close();
}).then((HttpClientResponse response) {
response.transform(utf8.decoder).listen((contents) {
//print(contents);
});
});
myhttp.get('www.thebluealliance.com', 80, '/api/v3/teams/0/simple')
.then((HttpClientRequest request) {
request.headers.set("accept", "application/json");
request.headers.set("X-TBA-Auth-Key", "XXXX");
return request.close();
})
.then((HttpClientResponse response) {
response.transform(utf8.decoder).transform(json.decoder).listen((data) {
print(data.toString());
setState(() {
TeamList myTeamsL = new TeamList.fromJson(data);
teamData = myTeamsL.teams;
});
});
});
}
}