从http请求数据时出错
Getting an error when requesting data from http
我不知道我需要做什么,我试图改变我的 URL。但我仍然收到此错误。
这里是错误。
Exception has occurred.
FormatException (FormatException: Unexpected character (at character 1)
<!DOCTYPE html>
^
)
下面是我请求数据的代码
Map<dynamic, dynamic> venuesData;
Future<void> fetchAndSetVenues() async {
try {
final response = await http.get(Uri.parse(venuesURL));
final extractedData = json.decode(response.body) as Map<dynamic, dynamic>;
venuesData = extractedData;
print(venuesData);
} catch (error) {
throw error;
}
}
// events
Map<dynamic, dynamic> eventsData;
Future<void> fetchAndSetEvents() async {
try {
final response = await http.get(Uri.parse(eventsURL));
final extractedData = json.decode(response.body) as Map<dynamic, dynamic>;
eventsData = extractedData;
} catch (error) {
throw error;
}
}
这是我的 URLs
String eventsURL = 'http://medeni.art/api/v1/events?page=1'
String venuesURL = 'http://medeni.art/api/v1/venues'
此错误发生在 json.decode
方法中。出于任何原因,如果 http 请求出错,则不应调用 json.decode
。您可以查看请求响应状态码。
import 'package:http/http.dart' as http;
var url = Uri.parse('https://example.com/whatsit/create');
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
if(response.statusCode ==200){
final result = json.decode(response.body);
}
我不知道我需要做什么,我试图改变我的 URL。但我仍然收到此错误。
这里是错误。
Exception has occurred.
FormatException (FormatException: Unexpected character (at character 1)
<!DOCTYPE html>
^
)
下面是我请求数据的代码
Map<dynamic, dynamic> venuesData;
Future<void> fetchAndSetVenues() async {
try {
final response = await http.get(Uri.parse(venuesURL));
final extractedData = json.decode(response.body) as Map<dynamic, dynamic>;
venuesData = extractedData;
print(venuesData);
} catch (error) {
throw error;
}
}
// events
Map<dynamic, dynamic> eventsData;
Future<void> fetchAndSetEvents() async {
try {
final response = await http.get(Uri.parse(eventsURL));
final extractedData = json.decode(response.body) as Map<dynamic, dynamic>;
eventsData = extractedData;
} catch (error) {
throw error;
}
}
这是我的 URLs
String eventsURL = 'http://medeni.art/api/v1/events?page=1'
String venuesURL = 'http://medeni.art/api/v1/venues'
此错误发生在 json.decode
方法中。出于任何原因,如果 http 请求出错,则不应调用 json.decode
。您可以查看请求响应状态码。
import 'package:http/http.dart' as http;
var url = Uri.parse('https://example.com/whatsit/create');
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
if(response.statusCode ==200){
final result = json.decode(response.body);
}