print(response.data.toString());
3.2 post请求var dio = Dio();
var response = await dio.post(‘https://xxx.com/test’, data: {‘id’: 12, ‘name’: ‘wendu’});
3.3 并发请求var dio = Dio();
var response = await Future.wait([dio.post(‘https://xxx.com/info’), dio.get(‘https://xxx.com/token’)]);
3.4 下载文件var dio = Dio();
var response = await dio.download(‘https://www.xxx.com/’, ‘/savePath’);
3.5 发送 FormDatavar dio = Dio();
var formData = FormData.fromMap({
‘name’: ‘wendux’,
‘age’: 25,
});
var response = await dio.post(‘https://www.xxx.com/info’, data: formData);
3.6 通过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);
3.7 监听发送(上传)数据进度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’);
},
);
四 示例
4.1 baseOptions配置请求 配置baseOptions
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,
));
get请求Response response;
response = await dio.get(’/article/list/0/json’);
print(response.data);
带options的get请求var responseMap = await dio.get(
‘/article/list/0/json’,
// Transform response data to Json Map
options: Options(responseType: ResponseType.json),
);
print(responseMap.data);
带参数的post请求Response response
response = await dio.post(
‘//user/login’,
data: {
最后送福利了,现在关注我并且加入群聊可以获取包含源码解析,自定义View,动画实现,架构分享等。
内容难度适中,篇幅精炼,每天只需花上十几分钟阅读即可。
大家可以跟我一起探讨,欢迎加群探讨,有flutter—底层开发—性能优化—移动架构—资深UI工程师 —NDK相关专业人员和视频教学资料,还有更多面试题等你来拿
点击GitHub领取
ub领取](https://github.com/a120464/Android-P7/blob/master/Android%E5%BC%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)**
[外链图片转存中…(img-3n95yTPL-1643788966126)]