forked from liujingxing/rxhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpSender.java
More file actions
107 lines (88 loc) · 3 KB
/
HttpSender.java
File metadata and controls
107 lines (88 loc) · 3 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package rxhttp;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import rxhttp.wrapper.ssl.HttpsUtils;
import rxhttp.wrapper.ssl.HttpsUtils.SSLParams;
import rxhttp.wrapper.utils.LogUtil;
/**
* 有公共参数的请求,用此类
* User: ljx
* Date: 2017/12/2
* Time: 11:13
*/
public final class HttpSender {
private static OkHttpClient mOkHttpClient; //只能初始化一次,第二次将抛出异常
public static void init(OkHttpClient okHttpClient, boolean debug) {
setDebug(debug);
init(okHttpClient);
}
public static void init(OkHttpClient okHttpClient) {
if (mOkHttpClient != null)
throw new IllegalArgumentException("OkHttpClient can only be initialized once");
mOkHttpClient = okHttpClient;
}
//判断是否已经初始化
public static boolean isInit() {
return mOkHttpClient != null;
}
public static OkHttpClient getOkHttpClient() {
if (mOkHttpClient == null)
mOkHttpClient = getDefaultOkHttpClient();
return mOkHttpClient;
}
public static OkHttpClient.Builder newOkClientBuilder() {
return getOkHttpClient().newBuilder();
}
public static void setDebug(boolean debug) {
LogUtil.setDebug(debug);
}
//所有的请求,最终都会调此方法拿到Call对象,然后执行请求
public static Call newCall(OkHttpClient client, Request request) {
return client.newCall(request);
}
/**
* 连接、读写超时均为10s、添加信任证书并忽略host验证
*
* @return 返回默认的OkHttpClient对象
*/
private static OkHttpClient getDefaultOkHttpClient() {
SSLParams sslParams = HttpsUtils.getSslSocketFactory();
return new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager) //添加信任证书
.hostnameVerifier((hostname, session) -> true) //忽略host验证
.build();
}
/**
* 取消所有请求
*/
static void cancelAll() {
final OkHttpClient okHttpClient = mOkHttpClient;
if (okHttpClient == null) return;
okHttpClient.dispatcher().cancelAll();
}
/**
* 根据Tag取消请求
*/
static void cancelTag(Object tag) {
if (tag == null) return;
final OkHttpClient okHttpClient = mOkHttpClient;
if (okHttpClient == null) return;
Dispatcher dispatcher = okHttpClient.dispatcher();
for (Call call : dispatcher.queuedCalls()) {
if (tag.equals(call.request().tag())) {
call.cancel();
}
}
for (Call call : dispatcher.runningCalls()) {
if (tag.equals(call.request().tag())) {
call.cancel();
}
}
}
}