classHttpRequest{ static final BaseOptions options = BaseOptions( baseUrl: HTTPConfig.baseURL, connectTimeout: HTTPConfig.timeout); static final Dio dio = Dio(options);
// 2.添加第一个拦截器 dio.interceptors.add(InterceptorsWrapper(onRequest: (options, handler) { // Do something before request is sent print( "\n================================= 请求数据 ================================="); print("method = ${options.method.toString()}"); print("url = ${options.uri.toString()}"); print("headers = ${options.headers}"); print("params = ${options.queryParameters}"); return handler.next(options); //continue // If you want to resolve the request with some custom data, // you can resolve a `Response` object eg: `handler.resolve(response)`. // If you want to reject the request with a error message, // you can reject a `DioError` object eg: `handler.reject(dioError)` }, onResponse: (response, handler) { // Do something with response data print( "\n================================= 响应数据开始 ================================="); print("code = ${response.statusCode}"); print("data = ${response.data}"); print( "================================= 响应数据结束 =================================\n"); return handler.next(response); // continue // If you want to reject the request with a error message, // you can reject a `DioError` object eg: `handler.reject(dioError)` }, onError: (DioError e, handler) { // Do something with response error print( "\n=================================错误响应数据 ================================="); print("type = ${e.type}"); print("message = ${e.message}"); print("stackTrace = ${e.stackTrace}"); print("\n"); return handler.next(e); //continue // If you want to resolve the request with some custom data, // you can resolve a `Response` object eg: `handler.resolve(response)`. }));