Flutter dio2

dio 版本为 2.1.2

0. 一个极简的示例

import 'package:dio/dio.dart';

void getHttp() async {
    try {
        Response response = await Dio().get("http://www.baidu.com");
        print(response);
    } catch (e) {
        print(e);
    }
}

1.0. GET 请求

Response response;
Dio dio = Dio();
response = await dio.get('http://api.muco.vip/test.php?age=18');
print(response.data.toString());

输出:

{"method":"get","number":1,"get":{"age":"18"},"request":{"age":"18"},"isAjax":false}

1.1. GET 请求

Response response;
Dio dio = Dio();
response = await dio.get("http://api.muco.vip/test.php", queryParameters: {"id": 6, "name": "muco"});
print(response.data.toString());

输出:

{"method":"get","number":2,"get":{"id":"6","name":"muco"},"request":{"id":"6","name":"muco"},"isAjax":false}

1.2. GET 请求

Response response;
Dio dio = Dio();
response = await dio.get("http://api.muco.vip/test.php?age=18", queryParameters: {"id": 6, "name": "muco"});
print(response.data.toString());

输出:

{"method":"get","number":3,"get":{"age":"18","id":"6","name":"muco"},"request":{"age":"18","id":"6","name":"muco"},"isAjax":false}

2.0. POST 请求

Response response;
Dio dio = Dio();
response = await dio.post('http://api.muco.vip/test.php',queryParameters: {"id": 6, "name": "muco"});
print(response.data.toString());

输出:

{"method":"post","number":0,"post":[],"request":{"id":"6","name":"muco"},"isAjax":false}

2.1. POST 请求

Response response;
Dio dio = Dio();
response = await dio.post('http://api.muco.vip/test.php?age=18',queryParameters: {"id": 6, "name": "muco"});
print(response.data.toString());

输出:

{"method":"post","number":0,"post":[],"request":{"age":"18","id":"6","name":"muco"},"isAjax":false}

2.2. POST 请求

我个人觉得这个POST 请求范例有问题,PHP后台压根接收不到数据,然而在其他接口上居然可以用上。。。厉害
Response response;
Dio dio = Dio();
response = await response = await dio.post('http://api.muco.vip/test.php',data: {"id": 6, "name": "muco"});
print(response.data.toString());

输出:

{"method":"post","number":0,"post":[],"request":[],"isAjax":false}