dio拦截器 flutter_史上最强大的Flutter Dio网络请求封装,Cookie管理,异常处理,文件下载,拦截器(注释完美,一看就懂)...

本文介绍了Flutter中Dio库的使用,包括网络请求的GET和POST方法,Cookie管理,异常处理,文件下载及拦截器的实现。通过示例代码详细展示了如何进行请求配置、错误处理和进度监听。
摘要由CSDN通过智能技术生成

import 'dart:io';

import 'package:dio/dio.dart';

import 'package:cookie_jar/cookie_jar.dart';

class HttpGo {

static final StringGET ="get";

static final StringPOST ="post";

static final StringDATA ="data";

static final StringCODE ="errorCode";

Diodio;

static HttpGo_instance;

BaseOptionsoptions;

static HttpGogetInstance() {

if (_instance ==null) {

_instance =HttpGo();

}

return _instance;

}

HttpGo() {

//BaseOptions、Options、RequestOptions 都可以配置参数,优先级别依次递增,且可以根据优先级别覆盖参数

options =new BaseOptions(

//请求基地址,可以包含子路径

baseUrl:"http://192.168.5.6:8085",

//Http请求头

headers: {'platform':'android', 'version':11.0},

//连接服务器超时时间,单位是毫秒.

connectTimeout:5000,

//响应流上前后两次接受到数据的间隔,单位为毫秒。

receiveTimeout:100000,

//请求的Content-Type,默认值是[ContentType.json]. 也可以用ContentType.parse("application/x-www-form-urlencoded")

contentType: ContentType.json,

//表示期望以那种格式(方式)接受响应数据。接受三种类型 `json`, `stream`, `plain`, `bytes`. 默认值是`json`,

responseType: ResponseType.plain,

);

dio =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('get success---------${response.statusCode}');

print('get success---------${response.data}');

//      response.data; 响应体

//      response.headers; 响应头

//      response.request; 请求体

//      response.statusCode; 状态码

}on DioErrorcatch (e) {

print('get error---------$e');

formatError(e);

}

return response.data;

}

/*

* post请求*/

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

Response response;

try {

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

print('post success---------${response.data}');

}on DioErrorcatch (e) {

print('post error---------$e');

formatError(e);

}

return response.data;

}

/*

* 下载文件*/

downloadFile(urlPath, savePath)async {

Response response;

try {

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

//进度

print("$count $total");

});

print('downloadFile success---------${response.data}');

}on DioErrorcatch (e) {

print('downloadFile error---------$e');

formatError(e);

}

return response.data;

}

/*

* error统一处理*/

void formatError(DioError e) {

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

// It occurs when url is opened timeout.

print("连接超时");

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

// It occurs when url is sent timeout.

print("请求超时");

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

//It occurs when receiving timeout

print("响应超时");

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

// When the server response, but with a incorrect status, such as 404, 503...

print("出现异常");

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

// When the request is cancelled, dio will throw a error with this type.

print("请求取消");

}else {

//DEFAULT Default error type, Some other Error. In this case, you can read the DioError.error if it is not null.

print("未知错误");

}

}

/*

* 取消请求*

* 同一个cancel token 可以用于多个请求,当一个cancel token取消时,所有使用该cancel token的请求都会被取消。  * 所以参数可选*/

void cancelRequests(CancelToken token) {

token.cancel("cancelled");

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值