這是我的代碼:
import 'package:http/http.dart' as http;
void getData() async {
print('getData called');
// Q is not a typo
String urlString = 'http://Qsamples.openweathermap.org/';
var url = Uri.parse(urlString);
http.Response response = await http.get(url);
if (response.statusCode == 200) {
print('statusCode = 200');
} else {
print('statusCode != 200');
}
}
當我向有效 URL 發出請求時,一切都像魅力一樣。但是當我將其更改為無效時,我得到
E/flutter(19828):[錯誤:flutter/lib/ui/ui_dart_state.cc(209)]未處理的例外:SocketException:主機查找失敗:'qsamples.openweathermap.org'(作業系統錯誤:沒有與主機名關聯的地址,errno = 7)
而不是接收response.statusCode == 404.
這里有一個圖書館作者的例子,他們建議相同的
您使用的 urlString 不起作用。urlString 將是:https ://samples.openweathermap.org/這個正在作業。
uj5u.com熱心網友回復:
嘗試更改您的網址:
http://Qsamples.openweathermap.org/
到 :
http://samples.openweathermap.org/q
它應該回傳 404 錯誤
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417690.html
標籤:
