前言
Dart通过Dio发送HTTP请求
下载依赖
Dart
Flutter
引入包
1
| import 'package:dio/dio.dart';
|
创建对象
发送简单的请求
发送GET请求
1 2 3
| void method() async { Response response = await dio.get("http://127.0.0.1:8080/"); }
|
请求行携带Param参数
直接拼接字符串
1 2 3
| void method() async { Response response = await dio.get("http://127.0.0.1:8080/?key1=value&key2=value"); }
|
通过对象携带参数
1 2 3 4 5 6
| void method() async { Response response = await dio.get("http://127.0.0.1:8080/", queryParameters: { "key1": "value", "key2": "value", }); }
|
发送POST请求
1 2 3
| void method() async { Response response = await dio.post("http://127.0.0.1:8080/"); }
|
请求体携带Data参数
JSON格式字符串
1 2 3 4 5 6
| void method() async { Response response = await dio.post("http://127.0.0.1:8080/", data: { "key1": "value", "key2": "value", }); }
|
1 2 3 4 5 6
| void method() async { Response response = await dio.post("http://127.0.0.1:8080/", data: FormData.fromMap({ "key1": "value", "key2": "value", })); }
|
发送复杂的请求
baseUrl:配置URL的主机地址部分,之后再通过dio对象发送请求时可以省略域名,直接访问资源路径
headers:定义请求头参数
validateStatus:定义哪些状态码被视为正常地请求,没有被视为正常的请求会抛异常,并且不会自动解析请求体的内容
method:请求方法
path:请求路径,如果指定了baseUrl,不需要再次指定域名部分
responseType:响应类型
json:缺省值
plain:纯文本
stream:二进制流,用来处理文件
queryParameters:请求行中的参数
data:请求体中的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| final dio = Dio(BaseOptions( baseUrl: "http://127.0.0.1:8080", headers: { 'Content-Type': 'application/json; charset=UTF-8', }, validateStatus: (int? status) { return true; }, method: "POST", path: "/", responseType: "json", queryParameters: {}, data: {}, ));
|
处理响应
得到响应状态码
得到响应体
转换为Map类型
1
| Map<String, dynamic> map = response.data;
|
转换为实体类型
结构体的属性为基本数据类型
响应数据案例
1 2 3 4
| { username: "", password: "" }
|
定义实体类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class User { late String? username; late String? password;
User({ this.username, this.password, });
factory User.fromJson(Map<String, dynamic> json) { return User( username: json["username"], password: json["password"], ); } }
|
Map转换为实体类型
1 2 3 4
| User user = User.fromJson(response.data);
String username = user.username; String password = user.password;
|
结构体的属性为其他实体类
响应数据案例
1 2 3 4 5 6
| { userObject: { username: "", password: "" } }
|
定义实体类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class UserObject { late String? username; late String? password;
UserObject({ this.username, this.password, });
factory UserObject.fromJson(Map<String, dynamic> json) { return UserObject( username: json["username"], password: json["password"], ); } }
class User { late UserObject? userObject;
User({ this.userObject, });
factory User.fromJson(Map<String, dynamic> json) { return User( username: UserObject.fromJson(json["userObject"]), ); } }
|
Map转换为实体类型
1 2 3 4
| User user = User.fromJson(response.data);
String username = user.userObject.username; String password = user.userObject.password;
|
结构体的属性为List集合
响应数据案例
1 2 3 4 5 6 7 8 9 10 11 12
| { userList: [ { username: "", password: "" }, { username: "", password: "" } ] }
|
定义实体类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| class UserObject { late String? username; late String? password;
UserObject({ this.username, this.password, });
factory UserObject.fromJson(Map<String, dynamic> json) { return UserObject( username: json["username"], password: json["password"], ); } }
class User { late List<UserObject> userList;
User({ this.userList, });
factory User.fromJson(Map<String, dynamic> json) { List<dynamic> userList = json["userList"] as List; List<UserObject> userObjectList = []; for (var user in userList) { userObjectList.add(UserObject.fromJson(user)); } return User( userList: userObjectList, ); } }
|
Map转换为实体类型
1 2 3 4 5 6
| User user = User.fromJson(response.data);
String username1 = user.userList[0].username; String password1 = user.userList[0].password; String username2 = user.userList[1].username; String password2 = user.userList[1].password;
|
完成
参考文献
Dio官方文档
Github——wendux
博客园——SevenNight
稀土掘金——Jam_Chan