打开CMD终端,执行如下指令(自动添加pubspec.yaml依赖)
Dart项目:
dart pub add dio
Flutter项目:
flutter pub add dio
Dart项目:
dart pub remove dio
Flutter项目:
flutter pub remove dio
flutter pub get
var dio = Dio();
var response = await dio.get('https://xxx.com/test?id=12&name=wendu');
print(response.data.toString());
response = await dio.get('https://xxx.com/test', queryParameters: {'id': 12, 'name': 'wendu'});
print(response.data.toString());
var dio = Dio();
var response = await dio.post('https://xxx.com/test', data: {'id': 12, 'name': 'wendu'});
var dio = Dio();
var response = await Future.wait([dio.post('https://xxx.com/info'), dio.get('https://xxx.com/token')]);
var dio = Dio();
var response = await dio.download('https://www.xxx.com/', '/savePath');
var dio = Dio();
var formData = FormData.fromMap({
'name': 'wendux',
'age': 25,
});
var response = await dio.post('https://www.xxx.com/info', data: formData);
var dio = Dio();
var formData = FormData.fromMap({
'name': 'wendux',
'age': 25,
'file': await MultipartFile.fromFile('./text.txt', filename: 'upload.txt'),
'files': [
await MultipartFile.fromFile('./text1.txt', filename: 'text1.txt'),
await MultipartFile.fromFile('./text2.txt', filename: 'text2.txt'),
]
});
var response = await dio.post('https://www.xxx.com/info', data: formData);
var dio = Dio();
var response = await dio.post(
'http://www.dtworkroom.com/doris/1/2.0.0/test',
data: {'aa': 'bb' * 22},
onSendProgress: (int sent, int total) {
print('$sent $total');
},
);
var dio = Dio(BaseOptions(
baseUrl: 'https://www.wanandroid.com/',
connectTimeout: 5000,
receiveTimeout: 100000,
headers: {
HttpHeaders.userAgentHeader: 'dio',
'api': '1.0.0',
},
contentType: Headers.jsonContentType,
responseType: ResponseType.plain,
));
Response response;
response = await dio.get('/article/list/0/json');
print(response.data);
var responseMap = await dio.get(
'/article/list/0/json',
// Transform response data to Json Map
options: Options(responseType: ResponseType.json),
);
print(responseMap.data);
Response response
response = await dio.post(
'//user/login',
data: {
'username': 'wanandroidUser1',
'password': '123456'
},
// Send data with "application/x-www-form-urlencoded" format
options: Options(
contentType: Headers.formUrlEncodedContentType,
),
);
print(response.data);
Response response;
response = await dio.fetch(
RequestOptions(path: 'https://www.wanandroid.com/article/list/0/json'),
);
print(response.data);
var dio = Dio(BaseOptions(
baseUrl: 'https://www.wanandroid.com/',
connectTimeout: 5000,
receiveTimeout: 100000,
headers: {
HttpHeaders.userAgentHeader: 'dio',
'api': '1.0.0',
},
contentType: Headers.jsonContentType,
responseType: ResponseType.plain,
));
Response response;
dio.interceptors.add(InterceptorsWrapper(
onRequest: (options, handler) {
return handler.next(options);
},
onResponse: (response, handler) {
return handler.next(response);
},
onError: (DioError e, handler) {
}));
response = await dio.post(
'/user/login',
data: {'username': 'wanandroidUser1', 'password': '123456'},
// Send data with "application/x-www-form-urlencoded" format
options: Options(
contentType: Headers.formUrlEncodedContentType,
),
);
print(response.data);
}