Skip to content

Commit 7d65361

Browse files
author
bulezeng(曾卫进)
committed
1 修改http 请求调用接口,使用okhttp
2 增加接口可以支持 修改 信鸽http 调用地址 3 修改json 序列化协议为 fastxml jackson
1 parent 0a56e6e commit 7d65361

25 files changed

+183
-178
lines changed

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@
103103
</dependency>
104104

105105
<dependency>
106-
<groupId>org.codehaus.jackson</groupId>
107-
<artifactId>jackson-core-asl</artifactId>
108-
<version>${jackson.version}</version>
106+
<groupId>com.fasterxml.jackson.core</groupId>
107+
<artifactId>jackson-databind</artifactId>
108+
<version>2.9.5</version>
109109
</dependency>
110110

111111
<dependency>
112-
<groupId>org.codehaus.jackson</groupId>
113-
<artifactId>jackson-mapper-asl</artifactId>
114-
<version>${jackson.version}</version>
112+
<groupId>com.squareup.okhttp3</groupId>
113+
<artifactId>okhttp</artifactId>
114+
<version>3.11.0</version>
115115
</dependency>
116116

117117
<dependency>

src/main/java/com/tencent/xinge/XingeApp.java

Lines changed: 43 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import java.net.URL;
88
import java.security.KeyManagementException;
99
import java.security.NoSuchAlgorithmException;
10+
import java.util.concurrent.TimeUnit;
1011

1112
import com.tencent.xinge.push.app.PushAppRequest;
13+
import okhttp3.*;
1214
import org.apache.commons.codec.binary.Base64;
1315
import org.json.JSONObject;
1416

15-
import com.tencent.xinge.api.RESTAPI_V3;
17+
import com.tencent.xinge.api.RESTAPIV3;
1618

1719
import javax.net.ssl.*;
1820

@@ -26,10 +28,17 @@
2628
*/
2729
public class XingeApp {
2830

31+
public static MediaType JSON_MEDIA_TYPE = MediaType.parse("application/json");
2932

3033
private String authString = null;
3134
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();
3342

3443
/**
3544
* HTTP Header Authorization 的值:Basic base64_auth_string<br>
@@ -46,21 +55,28 @@ public XingeApp(String appId, String secretKey) {
4655
authStringEnc = new String(authEncBytes);
4756
}
4857

58+
59+
4960
/**
5061
* @param authStringEnc base64_auth_string
5162
*/
5263
public XingeApp(String authStringEnc) {
5364
this.authStringEnc = authStringEnc;
5465
}
5566

67+
/**
68+
* 设置信鸽api http 服务器地址
69+
* @param url
70+
*/
71+
public void setDomainUrl(String url){
72+
restapiV3.setDomainUrl(url);
73+
}
5674
/**
5775
* @param jsonRequest jsonRequest
5876
* @return 通用基础返回值,是所有请求的响应中都会包含的字段
5977
*/
6078
public JSONObject pushApp(String jsonRequest) {
61-
62-
return callRestful(RESTAPI_V3.RESTAPI_PUSHSINGLEDEVICE, jsonRequest);
63-
79+
return callRestful(restapiV3.getRestApiPushUrl(), jsonRequest);
6480
}
6581

6682
/**
@@ -73,115 +89,45 @@ public JSONObject pushApp(PushAppRequest pushAppRequest) {
7389
return pushApp(jsonRequest);
7490
}
7591

76-
7792
/**
7893
*
7994
* @param jsonRequest jsonRequest
8095
* @return 通用基础返回值,是所有请求的响应中都会包含的字段
8196
*/
8297
public JSONObject deviceTag(String jsonRequest) {
83-
return callRestful(RESTAPI_V3.RESTAPI_TAG, jsonRequest);
98+
return callRestful(restapiV3.getRestApiTagUrl(), jsonRequest);
8499
}
85100

86101
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;
94102
JSONObject jsonRet = null;
95103

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();
111111

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+
}
144125
}
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-
152126
} catch (IOException e) {
153127
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);
164129
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-
}
183130
}
184-
185131
return jsonRet;
186132
}
187133

@@ -193,27 +139,5 @@ public static String stringifyError(Throwable error) {
193139
return result.toString();
194140
}
195141

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-
}
218142

219143
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.tencent.xinge.api;
2+
3+
/**
4+
* v3 REST API
5+
*
6+
*/
7+
public class RESTAPIV3 extends RESTAPI {
8+
9+
/**
10+
* 版本号 v3
11+
*/
12+
public static final String RESTAPI_VERSIONS = "v3";
13+
14+
/**
15+
* 接口域名
16+
*/
17+
public static final String RESTAPI_DOMAINS = "https://openapi.xg.qq.com/";
18+
19+
/**
20+
* 所有推送目标都采用相同的 URL 发起请求
21+
*/
22+
public static final String RESTAPI_PUSH =
23+
RESTAPI_DOMAINS + RESTAPI_VERSIONS + "/push/app";
24+
25+
/**
26+
* v3/tag
27+
*/
28+
public static final String RESTAPI_TAG =
29+
RESTAPI_DOMAINS + RESTAPI_VERSIONS + "/device/tag";
30+
31+
/**
32+
* api 请求地址
33+
*/
34+
String domainUrl = RESTAPI_DOMAINS;
35+
36+
/**
37+
* tag 请求地址
38+
*/
39+
String restApiTagUrl = RESTAPI_TAG;
40+
41+
/**
42+
* push 请求地址
43+
*/
44+
String restApiPushUrl = RESTAPI_PUSH;
45+
46+
public RESTAPIV3() {
47+
domainUrl = RESTAPI_DOMAINS;
48+
}
49+
50+
public RESTAPIV3(String baseUrl) {
51+
this.domainUrl = baseUrl;
52+
}
53+
54+
public void setDomainUrl(String baseUrl) {
55+
this.domainUrl = baseUrl;
56+
}
57+
58+
public String getRestApiTagUrl() {
59+
restApiTagUrl = domainUrl + RESTAPI_VERSIONS + "/device/tag";
60+
return restApiTagUrl;
61+
}
62+
63+
public String getRestApiPushUrl() {
64+
restApiPushUrl = domainUrl + RESTAPI_VERSIONS + "/push/app";
65+
return restApiPushUrl;
66+
}
67+
68+
69+
}

src/main/java/com/tencent/xinge/api/RESTAPI_V3.java

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.tencent.xinge.bean;
2+
3+
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.databind.JsonSerializer;
5+
import com.fasterxml.jackson.databind.SerializerProvider;
6+
7+
import java.io.IOException;
8+
9+
/**
10+
* Created by Administrator on 2018/11/6.
11+
*/
12+
public class EnumSerializer<T> extends JsonSerializer{
13+
@Override
14+
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
15+
if(o instanceof Enum){
16+
jsonGenerator.writeString(String.valueOf(o));
17+
}
18+
}
19+
20+
public static void main(String[] args){
21+
System.out.println(String.valueOf(Environment.product));
22+
}
23+
}

0 commit comments

Comments
 (0)