Flutter教程——Dio的封装

193 阅读2分钟

Dio _dio;

BaseOptions _options;

//单例模式,只创建一次实例

static HttpManager getInstance(){

if(null == _instance){

  _instance = new HttpManager();

  return _instance;

}

}

//构造函数

HttpManager(){

_options =new BaseOptions(

  baseUrl: _BASEURL,

  //连接时间为5秒

  connectTimeout: _CONNECTTIMEOUT,

  //响应时间为3秒

  receiveTimeout: _RECEIVETIMEOUT,

  //设置请求头

  headers: {

    "resource":"android"

  },

  //默认值是"application/json; charset=utf-8",Headers.formUrlEncodedContentType会自动编码请求体.

  contentType: Headers.formUrlEncodedContentType,

  //共有三种方式json,bytes(响应字节),stream(响应流),plain

  responseType: ResponseType.json

);

_dio = new Dio(_options);

//设置Cookie

_dio.interceptors.add(CookieManager(CookieJar()));



//添加拦截器

_dio.interceptors.add(InterceptorsWrapper(

  onRequest: (RequestOptions options){

    print("在请求之前的拦截信息");

    return options;

  },

  onResponse: (Response response){

    print("在响应之前的拦截信息");

    return response;

  },

  onError: (DioError e){

    print("在错误之前的拦截信息");

    return e;

  }

));

}

//get请求方法

get(url,{data,options,cancelToken}) async{

Response response;

try{

  response = await _dio.get(url,queryParameters: data,options: options,cancelToken: cancelToken);

  print('getHttp response: $response');

}on DioError catch(e){

  print('getHttp exception: $e');

  formatError(e);

}

return response;

}

//post请求

post(url,{params,options,cancelToken}) async{

Response response;

try{

  response = await _dio.post(url,queryParameters: params,options: options,cancelToken: cancelToken);

  print('postHttp response: $response');

}on DioError catch(e){

  print('postHttp exception: $e');

  formatError(e);

}

return response;

}

//post Form请求

postForm(url,{data,options,cancelToken}) async{

Response response;

try{

  response = await _dio.post(url,options: options,cancelToken: cancelToken,data: data);

  print('postHttp response: $response');

}on DioError catch(e){

  print('postHttp exception: $e');

  formatError(e);

}

return response;

}

//下载文件

downLoadFile(urlPath,savePath) async{

Response response;

try{

  response = await _dio.download(urlPath, savePath,onReceiveProgress: (int count,int total){

    print('$count $total');

  });

  print('downLoadFile response: $response');

}on DioError catch(e){

  print('downLoadFile exception: $e');

  formatError(e);

}

return response;

}

//取消请求

cancleRequests(CancelToken token){

token.cancel("cancelled");

}

void formatError(DioError e) {

if (e.type == DioErrorType.CONNECT_TIMEOUT) {

  print("连接超时");

} else if (e.type == DioErrorType.SEND_TIMEOUT) {

  print("请求超时");

} else if (e.type == DioErrorType.RECEIVE_TIMEOUT) {

  print("响应超时");

} else if (e.type == DioErrorType.RESPONSE) {

  print("出现异常");

} else if (e.type == DioErrorType.CANCEL) {

  print("请求取消");

} else {

  print("未知错误");

}

}

}




调用时只需:



http.HttpManager.getInstance().get('',params:{'key': 'free','msg': '鹅鹅鹅'});




输出:



2020-04-17 14:42:30.252 25364-25460/com.example.flutter\_module.host I/flutter: 在请求之前的拦截信息  

2020-04-17 14:42:31.145 25364-25460/com.example.flutter\_module.host I/flutter: 在响应之前的拦截信息  

2020-04-17 14:42:31.148 25364-25460/com.example.flutter\_module.host I/flutter: getHttp response: {"result":0,"content":"曲项向天歌"}



其实我觉得响应结果转为json就很好用了,如果喜欢转为实体类,自己再稍加封装就好。