-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpUtil.dart
More file actions
90 lines (80 loc) · 2.05 KB
/
HttpUtil.dart
File metadata and controls
90 lines (80 loc) · 2.05 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* 网络请求工具类
*/
import 'package:dio/dio.dart';
import 'BaseUrlUtil.dart';
class HttpUtil {
static HttpUtil instance;
Dio dio;
BaseOptions options;
static HttpUtil getInstance() {
if(instance == null){
instance = new HttpUtil();
}
return instance;
}
HttpUtil() {
Map<String, dynamic> optHeader = {
'traceinfo': 'versionName=2.6.1;versionCode=20190401;appName=%E5%81%A5%E5%AE%A2%E8%A1%8C;model=iPhone8,2;clientname=iPhone;channelId=10000;idfa=EE6C1572-06B5-4415-931A-247618068190;loginSource=2;deviceUuid=6815F5E3FE06436DBD6C0AD8DCFC970F;source=2;applicationCode=jkAgent;userId=8271;userToken=ed23c061d7e300672f7a83267463f902',
'user-agent': 'HotSpot',
'accept-language': 'zh-cn',
'content-type': 'application/json'
};
options = BaseOptions(
//连接服务器超时时间,单位是毫秒.
connectTimeout: 30000,
headers: optHeader
);
dio = new Dio(options);
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
// 设置代理
client.findProxy = (uri) {
return 'PROXY 192.168.36.86:8888';
};
};
}
// get
get<T>(url, {baseUrl, data, options, cancelToken}) async {
Response<T> response;
try{
if (baseUrl == null) {
dio.options.baseUrl = BaseUrl().BjUrl();
} else {
dio.options.baseUrl = baseUrl;
}
response = await dio.get<T>(
url,
queryParameters: data,
cancelToken: cancelToken
);
return response.data;
} on DioError catch(e){
return null;
}
}
// post
post<T>(url, {baseUrl, data, options, cancelToken}) async {
Response<T> response;
try{
if (baseUrl == null) {
dio.options.baseUrl = BaseUrl().BjUrl();
} else {
dio.options.baseUrl = baseUrl;
}
response = await dio.post<T>(
url,
data: data,
cancelToken: cancelToken
);
return response.data;
} on DioError catch(e){
return null;
}
}
}
/**
* 调用方式
* var result = await JkMethodChannel.platform.invokeMethod("getReqHeader");
* var response = await HttpUtil(result).get("", data: {"": "");
* print(response);
*/