从flutter中的API获取数据时发生格式异常



我收到了这个

"formatexception (formatexception: unexpected character (at line 8, character 1) <!doctype html><html class="no-js" lang="en"><head>"

错误,我的应用程序崩溃。我正试图从";http://newsapi.org/v2/top-headlines?country=ph&apiKey=aae937474dd8475ca6d45d403cbcd71c";API以前是有效的,但现在我遇到了异常错误。

这是我未来的代码,jsondecode就在这里。它在jsonDecode部分代码中出错。

String url =
"http://newsapi.org/v2/top-headlines?country=ph&apiKey=aae937474dd8475ca6d45d403cbcd71c";
var response = await http.get(url);
var jsonData = jsonDecode(response.body);
if (jsonData["status"] == "ok") {
jsonData["articles"].forEach((element) {
if (element["urlToImage"] != null && element["description"] != null) {
news.add(
Article(
title: element["title"],  
imgUrl: element["urlToImage"],
author: element["author"],
url: element["url"],
description: element["description"],
content: element["content"],
publishedAt: DateTime.parse(element["publishedAt"]),
),
);
}
});
}
}
}

您的请求返回错误页面请查看此示例,它将从NewsAPI获取新闻https://rrtutors.com/tutorials/Flutter-NewsApplication-ListVew-Search-Filter

最新更新