1
1
package com .tencent .xinge ;
2
2
3
- import com .google .common .base .Charsets ;
4
3
import java .io .*;
5
- import java .net .HttpURLConnection ;
6
- import java .net .MalformedURLException ;
7
- import java .net .URL ;
8
- import java .security .KeyManagementException ;
9
- import java .security .NoSuchAlgorithmException ;
4
+ import java .net .Proxy ;
10
5
import java .util .concurrent .TimeUnit ;
11
6
12
7
import com .tencent .xinge .push .app .PushAppRequest ;
16
11
17
12
import com .tencent .xinge .api .RESTAPIV3 ;
18
13
19
- import javax .net .ssl .*;
20
-
21
14
/**
22
15
* 提供V3接口<br>
23
16
* 1. v3中appId是String类型,v2中是long <br>
@@ -36,10 +29,7 @@ public class XingeApp {
36
29
37
30
private RESTAPIV3 restapiV3 = new RESTAPIV3 ();
38
31
39
- private OkHttpClient client = new OkHttpClient .Builder ()
40
- .connectTimeout (10 , TimeUnit .SECONDS )//设置连接超时时间
41
- .readTimeout (10 , TimeUnit .SECONDS )//设置读取超时时间
42
- .build ();
32
+ private OkHttpClient client ;
43
33
44
34
/**
45
35
* HTTP Header Authorization 的值:Basic base64_auth_string<br>
@@ -49,21 +39,40 @@ public class XingeApp {
49
39
* @param appId appId
50
40
* @param secretKey secretKey
51
41
*/
52
- public XingeApp (String appId , String secretKey ) {
42
+ private XingeApp (String appId , String secretKey ) {
53
43
this .authString = appId + ":" + secretKey ;
54
44
55
45
byte [] authEncBytes = Base64 .encodeBase64 (authString .getBytes ());
56
46
authStringEnc = new String (authEncBytes );
47
+
48
+ client = new OkHttpClient .Builder ()
49
+ .connectTimeout (10 , TimeUnit .SECONDS )//设置连接超时时间
50
+ .readTimeout (10 , TimeUnit .SECONDS )//设置读取超时时间
51
+ .build ();
52
+ }
53
+
54
+ private XingeApp (Builder builder ){
55
+ this .authString = builder .appId + ":" + builder .secretKey ;
56
+
57
+ byte [] authEncBytes = Base64 .encodeBase64 (authString .getBytes ());
58
+ authStringEnc = new String (authEncBytes );
59
+
60
+ client = new OkHttpClient .Builder ()
61
+ .proxy (builder .proxy )
62
+ .connectTimeout (builder .connectTimeOut , TimeUnit .SECONDS )//设置连接超时时间
63
+ .readTimeout (builder .readTimeOut , TimeUnit .SECONDS )//设置读取超时时间
64
+ .build ();
57
65
}
58
66
59
67
60
68
61
69
/**
62
70
* @param authStringEnc base64_auth_string
63
71
*/
72
+ /**
64
73
public XingeApp(String authStringEnc) {
65
74
this.authStringEnc = authStringEnc;
66
- }
75
+ }**/
67
76
68
77
/**
69
78
* 设置信鸽api http 服务器地址
@@ -141,5 +150,53 @@ public static String stringifyError(Throwable error) {
141
150
return result .toString ();
142
151
}
143
152
153
+ public static class Builder {
154
+ private String appId ;
155
+ private String secretKey ;
156
+ private Proxy proxy = Proxy .NO_PROXY ;
157
+ private int connectTimeOut ;
158
+ private int readTimeOut ;
159
+
160
+ public Builder () {
161
+ proxy = Proxy .NO_PROXY ;
162
+ connectTimeOut = 10 ;
163
+ readTimeOut = 10 ;
164
+ }
165
+
166
+ public Builder appId (String appId ) {
167
+ this .appId = appId ;
168
+ return this ;
169
+ }
170
+
171
+ public Builder secretKey (String secretKey ) {
172
+ this .secretKey = secretKey ;
173
+ return this ;
174
+ }
175
+
176
+ public Builder proxy (Proxy proxy ) {
177
+ this .proxy = proxy ;
178
+ return this ;
179
+ }
180
+
181
+ public Builder connectTimeOut (int connectTimeOut ) {
182
+ this .connectTimeOut = connectTimeOut ;
183
+ return this ;
184
+ }
185
+
186
+ public Builder readTimeOut (int readTimeOut ) {
187
+ this .readTimeOut = readTimeOut ;
188
+ return this ;
189
+ }
190
+
191
+ public XingeApp build () {
192
+ if (appId == null || secretKey == null ) {
193
+ throw new IllegalArgumentException ("Please set appId and secret key." );
194
+ }
195
+
196
+ return new XingeApp (this );
197
+ }
198
+
199
+ }
200
+
144
201
145
202
}
0 commit comments