7
7
import java .net .URL ;
8
8
import java .security .KeyManagementException ;
9
9
import java .security .NoSuchAlgorithmException ;
10
+ import java .util .concurrent .TimeUnit ;
10
11
11
12
import com .tencent .xinge .push .app .PushAppRequest ;
13
+ import okhttp3 .*;
12
14
import org .apache .commons .codec .binary .Base64 ;
13
15
import org .json .JSONObject ;
14
16
15
- import com .tencent .xinge .api .RESTAPI_V3 ;
17
+ import com .tencent .xinge .api .RESTAPIV3 ;
16
18
17
19
import javax .net .ssl .*;
18
20
26
28
*/
27
29
public class XingeApp {
28
30
31
+ public static MediaType JSON_MEDIA_TYPE = MediaType .parse ("application/json" );
29
32
30
33
private String authString = null ;
31
34
private String authStringEnc = null ;
32
- X509TrustManager trustManager = new DefaultX509TrustManager ();
35
+
36
+
37
+ RESTAPIV3 restapiV3 = new RESTAPIV3 ();
38
+ private OkHttpClient client = new OkHttpClient .Builder ()
39
+ .connectTimeout (10 , TimeUnit .SECONDS )//设置连接超时时间
40
+ .readTimeout (10 , TimeUnit .SECONDS )//设置读取超时时间
41
+ .build ();
33
42
34
43
/**
35
44
* HTTP Header Authorization 的值:Basic base64_auth_string<br>
@@ -46,21 +55,28 @@ public XingeApp(String appId, String secretKey) {
46
55
authStringEnc = new String (authEncBytes );
47
56
}
48
57
58
+
59
+
49
60
/**
50
61
* @param authStringEnc base64_auth_string
51
62
*/
52
63
public XingeApp (String authStringEnc ) {
53
64
this .authStringEnc = authStringEnc ;
54
65
}
55
66
67
+ /**
68
+ * 设置信鸽api http 服务器地址
69
+ * @param url
70
+ */
71
+ public void setDomainUrl (String url ){
72
+ restapiV3 .setDomainUrl (url );
73
+ }
56
74
/**
57
75
* @param jsonRequest jsonRequest
58
76
* @return 通用基础返回值,是所有请求的响应中都会包含的字段
59
77
*/
60
78
public JSONObject pushApp (String jsonRequest ) {
61
-
62
- return callRestful (RESTAPI_V3 .RESTAPI_PUSHSINGLEDEVICE , jsonRequest );
63
-
79
+ return callRestful (restapiV3 .getRestApiPushUrl (), jsonRequest );
64
80
}
65
81
66
82
/**
@@ -73,115 +89,45 @@ public JSONObject pushApp(PushAppRequest pushAppRequest) {
73
89
return pushApp (jsonRequest );
74
90
}
75
91
76
-
77
92
/**
78
93
*
79
94
* @param jsonRequest jsonRequest
80
95
* @return 通用基础返回值,是所有请求的响应中都会包含的字段
81
96
*/
82
97
public JSONObject deviceTag (String jsonRequest ) {
83
- return callRestful (RESTAPI_V3 . RESTAPI_TAG , jsonRequest );
98
+ return callRestful (restapiV3 . getRestApiTagUrl () , jsonRequest );
84
99
}
85
100
86
101
private synchronized JSONObject callRestful (String apiAddress , String jsonRequestString ) {
87
-
88
- URL url ;
89
- HttpsURLConnection https = null ;
90
- InputStreamReader isr = null ;
91
- BufferedReader br = null ;
92
- String ret = "" ;
93
- String temp ;
94
102
JSONObject jsonRet = null ;
95
103
96
- try {
97
- url = new URL (null , apiAddress , new sun .net .www .protocol .https .Handler ());
98
- https = (HttpsURLConnection ) url .openConnection ();
99
- https .setHostnameVerifier (new TrustAnyHostnameVerifier ());
100
- https .setRequestMethod (RESTAPI_V3 .HTTP_POST );
101
- https .setDoOutput (true );
102
- https .setDoInput (true );
103
- https .setUseCaches (false );
104
- https .setConnectTimeout (10000 );
105
- https .setReadTimeout (10000 );
106
- https .setRequestProperty ("Content-Type" , "application/json" );
107
- https .setRequestProperty ("Authorization" , "Basic " + authStringEnc );
108
- https .setRequestProperty ("Connection" , "Keep-Alive" );
109
-
110
- https .setSSLSocketFactory (this .getSSLSocketFactory ());
104
+ RequestBody requestBody = RequestBody .create (JSON_MEDIA_TYPE , jsonRequestString );
105
+ Request request = new Request .Builder ()
106
+ .url (apiAddress )
107
+ .addHeader ("Content-Type" , "application/json" )
108
+ .addHeader ("Authorization" , "Basic " + authStringEnc )
109
+ .post (requestBody )
110
+ .build ();
111
111
112
- byte [] out = jsonRequestString .getBytes (Charsets .UTF_8 );
113
- int length = out .length ;
114
-
115
- https .setFixedLengthStreamingMode (length );
116
-
117
- https .connect ();
118
-
119
- try {
120
- DataOutputStream os = new DataOutputStream (https .getOutputStream ());
121
- os .write (out );
122
- os .close ();
123
-
124
- } catch (Exception e ) {
125
-
126
- }
127
-
128
-
129
- https .getOutputStream ().flush ();
130
- https .getOutputStream ().close ();
131
-
132
- InputStream in = null ;
133
- if (https .getResponseCode () >= 400 ) {
134
- in = https .getErrorStream ();
135
- } else {
136
- in = https .getInputStream ();
137
- }
138
- isr = new InputStreamReader (in );
139
-
140
- isr = new InputStreamReader (in );
141
- br = new BufferedReader (isr );
142
- while ((temp = br .readLine ()) != null ) {
143
- ret += temp ;
112
+ try {
113
+ Response response = client .newCall (request ).execute ();
114
+ if (response .isSuccessful ()){
115
+ if (response .code () == 200 ){
116
+ String retMsg = response .body ().string ();
117
+ System .out .println (retMsg );
118
+ jsonRet = new JSONObject (retMsg );
119
+ }
120
+ else {
121
+ jsonRet = new JSONObject ();
122
+ jsonRet .put ("ret_code" , 10101 );
123
+ jsonRet .put ("err_msg" , "CallApiError,HttpStatus Code:" + response .code ());
124
+ }
144
125
}
145
- jsonRet = new JSONObject (ret );
146
-
147
- } catch (MalformedURLException e ) {
148
- jsonRet = new JSONObject ();
149
- jsonRet .put ("ret_code" , 10100 );
150
- jsonRet .put ("err_msg" , stringifyError (e ));
151
-
152
126
} catch (IOException e ) {
153
127
jsonRet = new JSONObject ();
154
- jsonRet .put ("ret_code" , 10101 );
155
- jsonRet .put ("err_msg" , stringifyError (e ));
156
-
157
- } catch (NoSuchAlgorithmException e ) {
158
- jsonRet = new JSONObject ();
159
- jsonRet .put ("ret_code" , 10102 );
160
- jsonRet .put ("err_msg" , stringifyError (e ));
161
- } catch (KeyManagementException e ) {
162
- jsonRet = new JSONObject ();
163
- jsonRet .put ("ret_code" , 10103 );
128
+ jsonRet .put ("ret_code" , 10100 );
164
129
jsonRet .put ("err_msg" , stringifyError (e ));
165
- } finally {
166
- if (br != null ) {
167
- try {
168
- br .close ();
169
- } catch (IOException e ) {
170
- // ignore
171
- }
172
- }
173
- if (isr != null ) {
174
- try {
175
- isr .close ();
176
- } catch (IOException e ) {
177
- // ignore
178
- }
179
- }
180
- if (https != null ) {
181
- https .disconnect ();
182
- }
183
130
}
184
-
185
131
return jsonRet ;
186
132
}
187
133
@@ -193,27 +139,5 @@ public static String stringifyError(Throwable error) {
193
139
return result .toString ();
194
140
}
195
141
196
- private SSLSocketFactory getSSLSocketFactory () throws KeyManagementException , NoSuchAlgorithmException {
197
- TrustManager [] tm = {this .trustManager };
198
- SSLContext sslContext = SSLContext .getInstance ("TLSv1" );
199
- sslContext .init (null , tm , new java .security .SecureRandom ());
200
- SSLSocketFactory ssf = sslContext .getSocketFactory ();
201
- return ssf ;
202
- }
203
-
204
- /**
205
- * 设置证书信任管理器
206
- * @param trustManager
207
- */
208
- public void setTrustManager (X509TrustManager trustManager ) {
209
- this .trustManager = trustManager ;
210
- }
211
-
212
- public class TrustAnyHostnameVerifier implements HostnameVerifier {
213
- public boolean verify (String hostname , SSLSession session ) {
214
- // 直接返回true
215
- return true ;
216
- }
217
- }
218
142
219
143
}
0 commit comments