forked from liujingxing/rxhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSender.java
More file actions
126 lines (107 loc) · 3.91 KB
/
Sender.java
File metadata and controls
126 lines (107 loc) · 3.91 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
package rxhttp;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import rxhttp.wrapper.callback.ProgressCallback;
import rxhttp.wrapper.param.IUploadLengthLimit;
import rxhttp.wrapper.param.Param;
import rxhttp.wrapper.progress.ProgressInterceptor;
import rxhttp.wrapper.utils.LogUtil;
/**
* User: ljx
* Date: 2018/1/10
* Time: 18:36
*/
class Sender {
private static Function<Param, Param> mOnParamAssembly;
private static OkHttpClient mOkHttpClient; //只能初始化一次,第二次将抛出异常
static void init(OkHttpClient okHttpClient) {
if (mOkHttpClient != null)
throw new IllegalArgumentException("OkHttpClient can only be initialized once");
mOkHttpClient = okHttpClient;
}
static OkHttpClient getOkHttpClient() {
if (mOkHttpClient == null)
mOkHttpClient = getDefaultOkHttpClient();
return mOkHttpClient;
}
static void setDebug(boolean debug) {
LogUtil.setDebug(debug);
}
static void setOnParamAssembly(Function<Param, Param> onParamAssembly) {
mOnParamAssembly = onParamAssembly;
}
/**
* 同步执行请求
*
* @param param 请求参数
* @return Http响应结果
* @throws IOException 超时、网络异常
*/
static Response execute(@NonNull Param param) throws IOException {
return newCall(param).execute();
}
static Call newCall(Param param) throws IOException {
return newCall(getOkHttpClient(), param);
}
//所有的请求,最终都会调此方法拿到Call对象,然后执行请求
static Call newCall(OkHttpClient client, Param param) throws IOException {
param = onAssembly(param);
LogUtil.log(param);
if (param instanceof IUploadLengthLimit) {
((IUploadLengthLimit) param).checkLength();
}
return client.newCall(param.buildRequest());
}
/**
* 克隆一个OkHttpClient对象,用于监听下载进度
*
* @param progressCallback 进度回调
* @return 克隆的OkHttpClient对象
*/
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();
}
/**
* <P>对Param参数添加一层装饰,可以在该层做一些与业务相关工作,
* <P>例如:添加公共参数/请求头信息
*
* @param p 参数
* @return 装饰后的参数
*/
private static Param onAssembly(Param p) throws IOException {
Function<Param, Param> f = mOnParamAssembly;
if (f == null) return p;
if (p == null || !p.isAssemblyEnabled()) return p;
try {
return f.apply(p);
} catch (Exception e) {
throw new IOException(e);
}
}
}