will-play/lib/utils/dioHttp.dart

102 lines
3.5 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:dio/dio.dart';
var token =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJ0ZXN0MSJ9.GV21nLXeLwZI7lB5Mp2lk9kEBJl4kJOPtw7YXwXINrE';
// Dio dio = new Dio();
// dio.options = BaseOptions(
// baseUrl: 'http://101.35.117.69:9093/',
// connectTimeout: 5000,
// receiveTimeout: 5000,
// headers: {"Authorization": 'Bearer ${token}'},
// contentType: Headers.formUrlEncodedContentType,
// );
// dio.Interceptors.add(
// InterceptorsWrapper(
// onRequest: (options, handler) {},
// onResponse: (response, handler) {},
// onError: (DioError e, handler) {},
// ),
// );
class dioHttp {
static final options = BaseOptions(
baseUrl: 'http://101.35.117.69:9093/',
connectTimeout: 5000,
receiveTimeout: 5000,
headers: {"Authorization": 'Bearer ${token}'},
);
Dio dio = new Dio(options);
}
// Dio dio = new Dio(
// BaseOptions(
// baseUrl: 'http://101.35.117.69:9093/',
// connectTimeout: 5000,
// receiveTimeout: 5000,
// headers: {"Authorization": 'Bearer ${token}'},
// contentType: Headers.formUrlEncodedContentType,
// ),
// Interceptors.add(
// InterceptorsWrapper(
// onRequest: (options, handler) {},
// onResponse: (response, handler) {},
// onError: (DioError e, handler) {},
// ),
// ),
// );
// //添加拦截器
// dio.interceptors.add(InterceptorsWrapper(
// onRequest:(options, handler){
// // Do something before request is sent
// return handler.next(options); //continue
// // 如果你想完成请求并返回一些自定义数据你可以resolve一个Response对象 `handler.resolve(response)`。
// // 这样请求将会被终止上层then会被调用then中返回的数据将是你的自定义response.
// //
// // 如果你想终止请求并触发一个错误,你可以返回一个`DioError`对象,如`handler.reject(error)`
// // 这样请求将被中止并触发异常上层catchError会被调用。
// },
// onResponse:(response,handler) {
// // Do something with response data
// return handler.next(response); // continue
// // 如果你想终止请求并触发一个错误,你可以 reject 一个`DioError`对象,如`handler.reject(error)`
// // 这样请求将被中止并触发异常上层catchError会被调用。
// },
// onError: (DioError e, handler) {
// // Do something with response error
// return handler.next(e);//continue
// // 如果你想完成请求并返回一些自定义数据可以resolve 一个`Response`,如`handler.resolve(response)`。
// // 这样请求将会被终止上层then会被调用then中返回的数据将是你的自定义response.
// }
// ));
// // 发起一个 GET 请求:
// Response response;
// var dio = Dio();
// response = await dio.get('/test?id=12&name=wendu');
// print(response.data.toString());
// // 上面的请求也可以这样做
// response = await dio.get('/test', queryParameters: {'id': 12, 'name': 'wendu'});
// print(response.data.toString());
// // 发起一个 POST 请求:
// response = await dio.post('/test', data: {'id': 12, 'name': 'wendu'});
// // 发起多个并发请求:
// response = await Future.wait([dio.post('/info'), dio.get('/token')]);
// // 下载文件:
// response = await dio.download('https://www.google.com/', './xx.html');
// // 发送 FormData:
// var formData = FormData.fromMap({
// 'name': 'wendux',
// 'age': 25,
// });
// var response = await dio.post('/info', data: formData);