import 'package:dio/dio.dart';
_loadDataGet() async {
try {
Response response = await Dio().get("https://www.baidu.com");
print(response);
} catch (e) {
print(e);
}
}
_loadDataPost() async {
try {
Response response = await Dio().post("path", data: {});
print(response);
} catch (e) {
print(e);
}
}
var dio = new Dio(BaseOptions(
baseUrl: "url",
connectTimeout: 5000,
receiveTimeout: 10000,
headers:{
HttpHeaders.userAgentHeader: "dio",
"api": "1.0.0",
},
contentType: ContentType.json,
responseType: ResponseType.plain
));
dio.interceptors.add(InterceptorsWrapper(
onRequest:(RequestOptions options) async {
// 请求发送前处理
return options; //continue
},
onResponse:(Response response) async {
// 在返回响应数据之前做一些预处理
return response; // continue
},
onError: (DioError e) async {
// 当请求失败时做一些预处理
return e;//continue
}
));
class HttpRequest {
static String _baseUrl = Api.BASE_URL;
static HttpRequest instance;
Dio dio;
BaseOptions options; // 基类请求配置,还可以使用Options单次请求配置,RequestOptions实际请求配置对多个域名发起请求
CancelToken cancelToken = CancelToken();
static HttpRequest getInstance() {
if (instance == null) {
instance = HttpRequest();
}
return instance;
}
}
HttpRequest() {
options = BaseOptions(
// 访问url
baseUrl: _baseUrl,
// 连接超时时间
connectTimeout: 5000,
// 响应流收到数据的间隔
receiveTimeout: 15000,
// http请求头
headers: {"version": "1.0.0"},
// 接收响应数据的格式
responseType: ResponseType.plain,
);
dio = Dio(options);
// 在拦截其中加入Cookie管理器
dio.interceptors.add(CookieManager(CookieJar()));
//添加拦截器
dio.interceptors.add(InterceptorsWrapper(onRequest: (RequestOptions options) {
// Do something before request is sent
return options; //continue
}, onResponse: (Response response) {
// Do something with response data
return response; // continue
}, onError: (DioError e) {
// Do something with response error
return e; //continue
}));
}
// cancelToken可用于多个请求,也可同时取消多个请求
void cancelRequests(CancelToken token) {
token.cancel("cancelled");
}
get(url,
{data,
options,
cancelToken,
BuildContext context,
Function successCallBack,
Function errorCallBack}) async {
Response response;
try {
response = await dio.get(url,
queryParameters: data, options: options, cancelToken: cancelToken);
} on DioError catch (e) {
handlerError(e);
}
if (response.data != null) {
BaseResponse baseResponse =
BaseResponse.fromJson(json.decode(response.data));
if (baseResponse != null) {
switch (baseResponse.errorCode) {
case 成功状态码:
successCallBack(jsonEncode(baseResponse.data));
break;
case 其他状态码:
errorCallBack(baseResponse.errorCode, baseResponse.errorMessage);
break;
default:
errorCallBack(baseResponse.errorCode, baseResponse.errorMessage);
break;
}
} else {
errorCallBack(Constants.NETWORK_JSON_EXCEPTION, "网络数据问题");
}
} else {
errorCallBack(Constants.NETWORK_ERROR, "网络出错啦,请稍后重试");
}
}
注意:
post请求
post(url,
{data,
options,
cancelToken,
BuildContext context,
Function successCallBack,
Function errorCallBack}) async {
Response response;
try {
response = await dio.post(url,
queryParameters: data, options: options, cancelToken: cancelToken);
} on DioError catch (e) {
handlerError(e);
}
if (response.data != null) {
BaseResponse baseResponse =
BaseResponse.fromJson(json.decode(response.data));
if (baseResponse != null) {
switch (baseResponse.errorCode) {
case 成功状态码:
successCallBack(jsonEncode(baseResponse.data));
break;
case 其他状态码:
errorCallBack(baseResponse.errorCode, baseResponse.errorMessage);
break;
default:
errorCallBack(baseResponse.errorCode, baseResponse.errorMessage);
break;
}
} else {
errorCallBack(Constants.NETWORK_JSON_EXCEPTION, "网络数据问题");
}
} else {
errorCallBack(Constants.NETWORK_ERROR, "网络出错啦,请稍后重试");
}
}
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 DioError catch (e) {
print('downloadFile error---------$e');
handlerError(e);
}
return response.data;
}
handlerError(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("未知错误");
}
}