class HttpUtlis {
static Dio _dio;
static BaseOptions _options = new BaseOptions(
connectTimeout: 5000,
receiveTimeout: 3000,
contentType: ContentType.json,
);
static get(String url, {options, Function success, Function failure}) async {
Dio dio = buildDio();
try {
Response response = await dio.get(url, options: options);
success(response.data);
} catch (exception) {
failure(exception);
}
}
static post(String url,
{params, options, Function success, Function failure}) async {
Dio dio = buildDio();
try {
Response response = await dio.post(url, data: params, options: options);
success(response.data);
} catch (exception) {
failure(exception);
}
}
static Dio buildDio() {
if (_dio == null) {
_dio = new Dio(_options);
}
return _dio;
}
}
使用
HttpUtlis.post("http://www.wanandroid.com/article/list/0/json", success: (value) {
print(value);
}, failure: (error) {
print(error);
});
网友评论