1 HttpClient
HttpClient是dart自带的请求类,在io包中,实现了基本的网络请求相关的操作。
网络调用通常遵循如下步骤:
- 创建 client.
- 构造 Uri.
- 发起请求, 等待请求,同时您也可以配置请求headers、 body。
- 关闭请求, 等待响应.
- 解码响应的内容.
HttpClient虽然可以发送正常的网络请求,但是会暴露过多的细节:
比如需要主动关闭request请求,拿到数据后也需要手动的进行字符串解码
void requestNetwork() async {
// 1.创建HttpClient对象
final httpClient = HttpClient();
// 2.构建请求的uri
final uri = Uri.parse("http://123.207.32.32:8000/api/v1/recommend");
// 3.构建请求
final request = await httpClient.getUrl(uri);
// 4.发送请求,必须
final response = await request.close();
if (response.statusCode == HttpStatus.ok) {
print(await response.transform(utf8.decoder).join());
} else {
print(response.statusCode);
}
}
2 http库
http 是 Dart 官方提供的另一个网络请求类,相比于 HttpClient,易用性提升了不少。
但是,没有默认集成到Dart的SDK中,所以我们需要先在pubspec中依赖它:
http:^0.12.0+2
import 'package:http/http.dart' as http;
class _MyHomeBodyState extends State<MyHomeBody> {
var msg = "";
@override
void initState() {
print("初始化");
http.get(Uri.parse("http://101.42.134.18:8080/hello")).then((response) {
setState(() {
msg = response.body;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text("$msg"),
);
}
}
3 dio三方库
dio是一个强大的Dart Http请求库,支持Restful API、FormData、拦截器、请求取消、Cookie管理、文件上传/下载、超时、自定义适配器等…
使用dio三方库必然也需要先在pubspec中依赖它:
dio:^3.0.1
import 'package:dio/dio.dart';
class _MyHomeBodyState extends State<MyHomeBody> {
var msg = "...";
@override
void initState() {
print("初始化");
Dio().get("http://101.42.134.18:8080/hello").then((response) {
setState(() {
msg = response.data.toString();
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text("$msg"),
);
}
}
(1) dio库的封装
import'package:dio/dio.dart';
import'package:testflutter001/service/config.dart';
class HTTPConfig {
static const baseURL = "https://httpbin.org";
static const timeout = 5000;
}
class HttpRequest {
static final BaseOptions options = BaseOptions(
baseUrl: HTTPConfig.baseURL, connectTimeout: HTTPConfig.timeout);
static final Dio dio = Dio(options);
static Future<T> request<T>(String url,
{String method = 'get', Map<String, dynamic> params, Interceptor inter}) async {
// 1.请求的单独配置
final options = Options(method: method);
// 2.添加第一个拦截器
Interceptor dInter = InterceptorsWrapper(
onRequest: (RequestOptions options) {
// 1.在进行任何网络请求的时候, 可以添加一个loading显示
// 2.很多页面的访问必须要求携带Token,那么就可以在这里判断是有Token
// 3.对参数进行一些处理,比如序列化处理等
print("拦截了请求");
return options;
},
onResponse: (Response response) {
print("拦截了响应");
return response;
},
onError: (DioError error) {
print("拦截了错误");
return error;
}
);
List<Interceptor> inters = [dInter];
if (inter != null) {
inters.add(inter);
}
dio.interceptors.addAll(inters);
// 3.发送网络请求
try {
Response response = await dio.request<T>(url, queryParameters: params, options: options);
return response.data;
} on DioError catch(e) {
return Future.error(e);
}
}
}
调用
HttpRequest.request("https://httpbin.org/get",
params: {"name": "why", 'age': 18})
.then((res) {
print(res);
});
HttpRequest.request("https://httpbin.org/post",
method: "post",
params: {"name": "why", 'age': 18})
.then((res) {
print(res);
});