处理异常 HTTP 请求 flutter
Handling exception HTTP request flutter
我想处理带有一些错误消息的 HTTP 请求抖动,但我在这里遇到了很多错误。我只是根据建议制作的,但它对我不起作用。请任何人帮助我
这是我调用 API
的函数
getData(data, apiUrl) async {
var tempUrl = _url + apiUrl + await _getToken();
Uri uri = Uri.parse(tempUrl);
var fullUrl = uri.replace(queryParameters: data);
var res;
try {
var response = await http.get(fullUrl, headers: _setHeaders()).timeout(
const Duration(seconds: 60));
print(response.statusCode);
if (response.statusCode != 200) {
res = {
"success": false,
"status": response.statusCode,
"message": _returnResponse(response)
};
}
else {
res = response;
}
}
on SocketException {
throw FetchDataException('No Internet connection');
}
on TimeoutException catch (e) {
res = {
"success": false,
"status": response.statusCode,
"message": "Connection timeout"
};
} on Error catch (e) {
print('Error: $e');
}
return res;
}
这是我对除 200
以外其他人的 return 回复
dynamic _returnResponse(http.Response response) {
switch (response.statusCode) {
case 400:
throw BadRequestException(response.body.toString());
case 401:
case 403:
throw UnauthorisedException(response.body.toString());
case 500:
default:
throw FetchDataException(
'Error occured while Communication with Server with StatusCode : ${response
.statusCode}');
}
}
这是我的 app_exception.dart 我从 Whosebug 和其他论坛
class AppException implements Exception {
final _message;
final _prefix;
AppException([this._message, this._prefix]);
String toString() {
return "$_prefix$_message";
}
}
class FetchDataException extends AppException {
FetchDataException([String message])
: super(message, "Error During Communication: ");
}
class BadRequestException extends AppException {
BadRequestException([message]) : super(message, "Invalid Request: ");
}
class UnauthorisedException extends AppException {
UnauthorisedException([message]) : super(message, "Unauthorised: ");
}
class InvalidInputException extends AppException {
InvalidInputException([String message]) : super(message, "Invalid Input: ");
}
我尝试了很多建议,但根本没有用
我遇到了这个错误
Error: 'SocketException' isn't a type.
on SocketException {
^^^^^^^^^^^^^^^
Error: 'TimeoutException' isn't a type.
on TimeoutException catch (e) {
^^^^^^^^^^^^^^^^
我使用了 dio 包。这比我做的更容易,错误更少
如果发生的错误是 SocketException 和 Timeout 异常,请确保您已在该文件中分别导入了 dart.io
和 dart.async
。关于您的代码,我能够成功 运行 它,但您可以参考 answer by Paresh Mangukiya for a step by step or refer here 以进一步了解如何在 flutter 中使用自定义错误响应处理网络调用和异常。
我想处理带有一些错误消息的 HTTP 请求抖动,但我在这里遇到了很多错误。我只是根据建议制作的,但它对我不起作用。请任何人帮助我 这是我调用 API
的函数getData(data, apiUrl) async {
var tempUrl = _url + apiUrl + await _getToken();
Uri uri = Uri.parse(tempUrl);
var fullUrl = uri.replace(queryParameters: data);
var res;
try {
var response = await http.get(fullUrl, headers: _setHeaders()).timeout(
const Duration(seconds: 60));
print(response.statusCode);
if (response.statusCode != 200) {
res = {
"success": false,
"status": response.statusCode,
"message": _returnResponse(response)
};
}
else {
res = response;
}
}
on SocketException {
throw FetchDataException('No Internet connection');
}
on TimeoutException catch (e) {
res = {
"success": false,
"status": response.statusCode,
"message": "Connection timeout"
};
} on Error catch (e) {
print('Error: $e');
}
return res;
}
这是我对除 200
以外其他人的 return 回复dynamic _returnResponse(http.Response response) {
switch (response.statusCode) {
case 400:
throw BadRequestException(response.body.toString());
case 401:
case 403:
throw UnauthorisedException(response.body.toString());
case 500:
default:
throw FetchDataException(
'Error occured while Communication with Server with StatusCode : ${response
.statusCode}');
}
}
这是我的 app_exception.dart 我从 Whosebug 和其他论坛
class AppException implements Exception {
final _message;
final _prefix;
AppException([this._message, this._prefix]);
String toString() {
return "$_prefix$_message";
}
}
class FetchDataException extends AppException {
FetchDataException([String message])
: super(message, "Error During Communication: ");
}
class BadRequestException extends AppException {
BadRequestException([message]) : super(message, "Invalid Request: ");
}
class UnauthorisedException extends AppException {
UnauthorisedException([message]) : super(message, "Unauthorised: ");
}
class InvalidInputException extends AppException {
InvalidInputException([String message]) : super(message, "Invalid Input: ");
}
我尝试了很多建议,但根本没有用
我遇到了这个错误
Error: 'SocketException' isn't a type. on SocketException { ^^^^^^^^^^^^^^^
Error: 'TimeoutException' isn't a type. on TimeoutException catch (e) { ^^^^^^^^^^^^^^^^
我使用了 dio 包。这比我做的更容易,错误更少
如果发生的错误是 SocketException 和 Timeout 异常,请确保您已在该文件中分别导入了 dart.io
和 dart.async
。关于您的代码,我能够成功 运行 它,但您可以参考 answer by Paresh Mangukiya for a step by step or refer here 以进一步了解如何在 flutter 中使用自定义错误响应处理网络调用和异常。