在Flutter中用dio发起HTTP网络请求
【摘要】 用dart io中的HttpClient发起的请求,但HttpClient本身功能较弱,很多常用功能都不支持。所以推荐大家使用dio 来发起网络请求,它是一个强大易用的dart http请求库,支持Restful API、FormData、拦截器、请求取消、Cookie管理、文件上传/下载。github dio
1.添加依赖
在pubspec.yaml加入以下依赖:...
用dart io中的HttpClient
发起的请求,但HttpClient
本身功能较弱,很多常用功能都不支持。所以推荐大家使用dio
来发起网络请求,它是一个强大易用的dart http请求库,支持Restful API、FormData、拦截器、请求取消、Cookie管理、文件上传/下载。github dio
1.添加依赖
在pubspec.yaml加入以下依赖:
dependencies:
dio: ^3.0.10 #latest version
2.下载依赖
在项目根目录下执行flutter pub get
:
~/AndroidStudioProjects/ME/startup_namer$ flutter pub get
3.网络请求
3.1.GET
Response response;
Dio dio = new Dio();
response = await dio.get("/test?id=12&name=wendu");
print(response.data.toString());
// Optionally the request above could also be done as
response = await dio.get("/test", queryParameters: {"id": 12, "name": "wendu"});
print(response.data.toString());
3.2.POST
response = await dio.post("/test", data: {"id": 12, "name": "wendu"});
3.3.并发网络请求
response = await Future.wait([dio.post("/info"), dio.get("/token")]);
3.4.下载文件
response = await dio.download("https://www.google.com/", "./xx.html");
3.5.响应流
获得流数据。
Response<ResponseBody> rs = await Dio().get<ResponseBody>(url,
options: Options(responseType: ResponseType.stream), // set responseType to `stream`
);
print(rs.data.stream); //response stream
3.6.用字节响应
获得字节数据。
Response<List<int>> rs = await Dio().get<List<int>>(url,
options: Options(responseType: ResponseType.bytes), // // set responseType to `bytes`
);
print(rs.data); // List<int>
3.7.发送表单数据
FormData formData = new FormData.fromMap({ "name": "wendux", "age": 25,
});
response = await dio.post("/info", data: formData);
3.8.用表单上传多个文件
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"), ]
});
response = await dio.post("/info", data: formData);
3.9.监听上传进度
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");
},
);
3.10.通过流发送二进制数据
// Binary data
List<int> postData = <int>[...];
await dio.post(
url,
data: Stream.fromIterable(postData.map((e) => [e])), //create a Stream<List<int>>
options: Options( headers: { Headers.contentLengthHeader: postData.length, // set content-length },
),
);
3.11.创建Dio实例,并配置它
Dio dio = new Dio(); // with default Options
// Set default configs
dio.options.baseUrl = "https://www.xx.com/api";
dio.options.connectTimeout = 5000; //5s
dio.options.receiveTimeout = 3000;
// or new Dio with a BaseOptions instance.
BaseOptions options = new BaseOptions( baseUrl: "https://www.xx.com/api", connectTimeout: 5000, receiveTimeout: 3000,
);
Dio dio = new Dio(options);
可以通过Options配置各种请求的信息,如Header。
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/108113973
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)