我正在使用 flutter android 模拟器来运行我的应用程序。应用程序使用 HTTP 发布请求将 json 文件发送到 API。现在,我在向 API 发送请求时遇到了问题,因为它显示了连接问题。 API是用flask构建的。
这里是 HTTP POST 请求的代码:
Future<PredictionModel> predictionOutput(
double aveHomeGoal,
double aveAwayGoal,
int homeWinPercentage,
int awayWinPercentage,
int homeDrawPercentage,
int awayDrawPercentage,
int homeRating,
int awayRating,
int homePost,
int awayPost,
int postDiff) async {
var headers = {'Content-Type': 'application/json'};
var request =
http.Request('POST', Uri.parse('http://127.0.0.1:5000/predict'));
request.body =
'''{"inputs":[ aveHomeGoal, aveAwayGoal, homeWinPercentage, awayWinPercentage, homeDrawPercentage, awayDrawPercentage, homeRating, awayRating, homePost, awayPost, postDiff]}''';
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
print(await response.stream.bytesToString());
} else {
print(response.reasonPhrase);
}
这是我点击 POST 请求时得到的错误:
E/flutter (17691):[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:SocketException:操作系统错误:连接被拒绝,errno = 111,地址 = 127.0.0.1,端口 = 59892
这是 Postman 的屏幕截图,显示 API 正在运行: