欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Flutter开发之——网络请求-Dio,android移动开发基础案例代码

时间:2023-07-31

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 发送 FormData

var 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)]

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。