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
161 lines (138 loc) · 4.99 KB
/
HttpSender.java
File metadata and controls
161 lines (138 loc) · 4.99 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package rxhttp;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import okhttp3.Call;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import rxhttp.wrapper.annotations.NonNull;
import rxhttp.wrapper.callback.ProgressCallback;
import rxhttp.wrapper.param.FormParam;
import rxhttp.wrapper.param.Param;
import rxhttp.wrapper.parse.DownloadParser;
import rxhttp.wrapper.parse.Parser;
import rxhttp.wrapper.progress.ProgressInterceptor;
import rxhttp.wrapper.ssl.SSLSocketFactoryImpl;
import rxhttp.wrapper.ssl.X509TrustManagerImpl;
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 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);
}
/**
* 同步发送一个请求
* <p>支持任意请求方式,如:Get、Head、Post、Put等
*
* @param param 请求参数
* @return Response
* @throws IOException 数据解析异常、网络异常等
*/
public static Response execute(@NonNull Param param) throws IOException {
return newCall(param.buildRequest()).execute();
}
/**
* 同步发送一个请求
* <p>支持任意请求方式,如:Get、Head、Post、Put等
* <p>亦支持文件上传/下载(无进度回调)
* {@link DownloadParser} 文件下载(无进度回调)
* {@link FormParam} 文件上传(无进度回调)
*
* @param param 请求参数
* @param parser 数据解析器
* @param <T> 要转换的目标数据类型
* @return T
* @throws IOException 数据解析异常、网络异常等
*/
public static <T> T execute(@NonNull Param param, @NonNull Parser<T> parser) throws IOException {
return parser.onParse(execute(param));
}
public static Call newCall(Request request) {
return newCall(getOkHttpClient(), request);
}
//所有的请求,最终都会调此方法拿到Call对象,然后执行请求
public static Call newCall(OkHttpClient client, Request request) {
return client.newCall(request);
}
/**
* 克隆一个OkHttpClient对象,用于监听下载进度
*
* @param progressCallback 进度回调
* @return 克隆的OkHttpClient对象
*/
public static OkHttpClient clone(@NonNull final ProgressCallback progressCallback) {
//克隆一个OkHttpClient后,增加拦截器,拦截下载进度
return getOkHttpClient().newBuilder()
.addNetworkInterceptor(new ProgressInterceptor(progressCallback))
.build();
}
/**
* 连接、读写超时均为10s、添加信任证书并忽略host验证
*
* @return 返回默认的OkHttpClient对象
*/
private static OkHttpClient getDefaultOkHttpClient() {
X509TrustManager trustAllCert = new X509TrustManagerImpl();
SSLSocketFactory sslSocketFactory = new SSLSocketFactoryImpl(trustAllCert);
return new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.sslSocketFactory(sslSocketFactory, trustAllCert) //添加信任证书
.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();
}
}
}
}