引入三方库
connectivity: ^0.4.5+2 #网络监测
device_info: ^0.4.0+1
package_info: ^0.4.0+6
dio: ^3.0.9
代码实现:
import 'dart:io';
import 'dart:collection';
import 'package:connectivity/connectivity.dart';
import 'package:dio/dio.dart';
import 'package:device_info/device_info.dart';
import 'package:package_info/package_info.dart';
class HttpManager {
static const String baseUrl = "";
static Map<String, String> baseHeaders;
static const ContentTypeJson = "application/json";
static const ContentTypeForm = "application/x-www-form-urlencoded";
static const ContentTypeImg = "image/jpeg";
//PUT 请求
static requestPutAction(url, params, File file) async {
List<int> bytes = await file.readAsBytes();
HttpClient httpClient = HttpClient();
HttpClientRequest request = await httpClient.putUrl(Uri.parse(url));
request.add(bytes);
HttpClientResponse response = await request.close();
if (response.statusCode == HttpStatus.ok) {
return ResultModel(null, true, response.statusCode,
headers: response.headers);
}
return ResultModel(null, false, response.statusCode,
headers: response.headers);
}
static requestPostAction(url, params, {noTip = false}) async {
RequestOptions options = new RequestOptions(method: "POST");
// return await
}
static requestBaseAction(
url, params, Map<String, String> header, RequestOptions option,
{noTip = false,
mybaseurl = baseUrl,
mycontentType = ContentTypeForm,
var formData}) async {
//判断网络
var connectivityResult = await (new Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
} else if (connectivityResult == ConnectivityResult.wifi) {
} else if (connectivityResult == ConnectivityResult.none) {
return ResultModel(
ResultErrorEvent(ResultCode.NETWORK_ERROR, "check network"),
false,
ResultCode.NETWORK_ERROR);
}
//出来请求头
Map<String, String> headers = new HashMap();
if (headers != null) {
headers.addAll(header);
}
Map<String, String> deviceHeaders;
PackageInfo packageInfo = await PackageInfo.fromPlatform();
if (Platform.isIOS) {
IosDeviceInfo info = await getDeviceInfo();
deviceHeaders = {
"deviceId": info.identifierForVendor,
"systemName": info.systemName,
"systemVersion": info.systemVersion,
"devideType": info.utsname.machine,
"release": info.utsname.release,
"version": packageInfo.version,
"packageName": packageInfo.packageName,
"buildNumber": packageInfo.buildNumber,
};
} else if (Platform.isAndroid) {
AndroidDeviceInfo info = await getDeviceInfo();
deviceHeaders = {
"deviceId": info.androidId,
"systemName": 'Android',
"systemVersion": info.product,
"phoneType": info.model,
"release": info.version.release,
"version": packageInfo.version,
"packageName": packageInfo.packageName,
"buildNumber": packageInfo.buildNumber,
};
}
if (formData == null) {
headers.addAll(deviceHeaders);
} else {
option = RequestOptions(method: "GET");
option.headers = headers;
}
option.baseUrl = mybaseurl;
option.connectTimeout = 15000;
headers.addAll({'content-type': mycontentType});
//新建dio
var dio = new Dio();
Response response;
try {
if (option.method == 'GET') {
response =
await dio.request(url, queryParameters: params, options: option);
} else if (option.method == 'PUT') {
response = await dio.request(url, data: formData, options: option);
} else {
response = await dio.request(url, data: params, options: option);
}
} on DioError catch (error) {
//请求错误处理
Response errorResponse;
if (error.response != null) {
errorResponse = error.response;
} else {
errorResponse = new Response(statusCode: 999);
}
if (error.type == DioErrorType.CONNECT_TIMEOUT) {
errorResponse.statusCode = ResultCode.NETWORK_TIMEOUT;
}
return ResultModel(response.data, false, response.statusCode,
headers: response.headers);
}
try {
if (response.statusCode == 200 || response.statusCode == 201) {
if (response.data.runtimeType != String) {
if (response.data['code'] == -2) {
//退出
return ResultModel(response.data, false, response.statusCode,
headers: response.headers);
}
}
return ResultModel(response.data, true, ResultCode.SUCCESS,
headers: response.headers);
}
} catch (error) {
return ResultModel(response.data, false, response.statusCode,
headers: response.headers);
}
return ResultModel(response.data, false, response.statusCode,
headers: response.headers);
}
static Future getDeviceInfo() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
return await deviceInfo.androidInfo;
} else if (Platform.isIOS) {
return await deviceInfo.iosInfo;
} else {
return null;
}
}
}
class ResultModel {
var data;
bool success;
int code;
var headers;
ResultModel(this.data, this.success, this.code, {this.headers});
}
class ResultCode {
static const NETWORK_ERROR = -1;
static const NETWORK_TIMEOUT = -2;
static const NETWORK_JSON_EXCEPTION = -3;
static const SUCCESS = 200;
}
class ResultErrorEvent {
final int code;
final String message;
ResultErrorEvent(this.code, this.message);
}
class Config {
static bool debug = false;
}