【发布时间】:2020-04-07 14:27:14
【问题描述】:
我正在创建一个新闻应用程序,我通过 JSON 获取我的数据,并且在每次运行我的应用程序时都会让我崩溃 (response HTTP) 我无法理解:
Exception has occurred. FormatException (FormatException: Unexpected character (at character 1 <HTML></HTML> ^ )
代码:
import 'package:http/http.dart' as http;
import 'dart:convert';
class Category {
int id;
String title;
Category({
this.id,
this.title
});
static Future<List<Category>> getCategories() async {
http.Response response = await http.get("url JSON"); // <- Here carash
Map<String, dynamic> map = json.decode(response.body);
List<Category> list = [];
for(var map in map['categories']){
list.add(
Category(id: map['id'], title: map['title'])
);
}
return list;
}
}
【问题讨论】:
-
您确定您的 JSON URL 返回的是 JSON,而不是 HTML?