Skip to content

Commit a294bc4

Browse files
committed
修复部分可能造成resource leak的代码
1 parent 4c98a56 commit a294bc4

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import java.security.NoSuchAlgorithmException;
4+
import java.util.Arrays;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
39
import com.google.gson.JsonElement;
410
import com.google.gson.JsonObject;
511
import com.google.gson.JsonParser;
612
import com.google.gson.JsonPrimitive;
7-
import com.google.gson.internal.Streams;
813
import com.google.gson.reflect.TypeToken;
9-
import com.google.gson.stream.JsonReader;
14+
1015
import me.chanjar.weixin.common.bean.WxCardApiSignature;
1116
import me.chanjar.weixin.common.bean.result.WxError;
1217
import me.chanjar.weixin.common.exception.WxErrorException;
@@ -17,12 +22,6 @@
1722
import me.chanjar.weixin.mp.api.WxMpService;
1823
import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
1924
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
23-
import java.io.StringReader;
24-
import java.security.NoSuchAlgorithmException;
25-
import java.util.Arrays;
2625

2726
/**
2827
* Created by Binary Wang on 2016/7/27.
@@ -76,7 +75,7 @@ public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
7675
if (this.wxMpService.getWxMpConfigStorage().isCardApiTicketExpired()) {
7776
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card";
7877
String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null);
79-
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
78+
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
8079
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
8180
String cardApiTicket = tmpJsonObject.get("ticket").getAsString();
8281
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
@@ -136,7 +135,7 @@ public String decryptCardCode(String encryptCode) throws WxErrorException {
136135
JsonObject param = new JsonObject();
137136
param.addProperty("encrypt_code", encryptCode);
138137
String responseContent = this.wxMpService.post(url, param.toString());
139-
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
138+
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
140139
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
141140
JsonPrimitive jsonPrimitive = tmpJsonObject.getAsJsonPrimitive("code");
142141
return jsonPrimitive.getAsString();
@@ -158,7 +157,7 @@ public WxMpCardResult queryCardCode(String cardId, String code, boolean checkCon
158157
param.addProperty("code", code);
159158
param.addProperty("check_consume", checkConsume);
160159
String responseContent = this.wxMpService.post(url, param.toString());
161-
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
160+
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
162161
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
163162
new TypeToken<WxMpCardResult>() {
164163
}.getType());
@@ -217,7 +216,7 @@ public void markCardCode(String code, String cardId, String openId, boolean isMa
217216
param.addProperty("openid", openId);
218217
param.addProperty("is_mark", isMark);
219218
String responseContent = this.wxMpService.post(url, param.toString());
220-
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
219+
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
221220
WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
222221
new TypeToken<WxMpCardResult>() { }.getType());
223222
if (!cardResult.getErrorCode().equals("0")) {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpGroupServiceImpl.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import java.util.List;
4+
35
import com.google.gson.JsonElement;
46
import com.google.gson.JsonObject;
5-
import com.google.gson.internal.Streams;
7+
import com.google.gson.JsonParser;
68
import com.google.gson.reflect.TypeToken;
7-
import com.google.gson.stream.JsonReader;
9+
810
import me.chanjar.weixin.common.exception.WxErrorException;
911
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
1012
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
@@ -14,9 +16,6 @@
1416
import me.chanjar.weixin.mp.bean.WxMpGroup;
1517
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
1618

17-
import java.io.StringReader;
18-
import java.util.List;
19-
2019
/**
2120
* Created by Binary Wang on 2016/7/21.
2221
*/
@@ -51,7 +50,7 @@ public List<WxMpGroup> groupGet() throws WxErrorException {
5150
* 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} }
5251
* 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] }
5352
*/
54-
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
53+
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
5554
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("groups"),
5655
new TypeToken<List<WxMpGroup>>() {
5756
}.getType());
@@ -63,7 +62,7 @@ public long userGetGroup(String openid) throws WxErrorException {
6362
JsonObject o = new JsonObject();
6463
o.addProperty("openid", openid);
6564
String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, o.toString());
66-
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
65+
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
6766
return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid"));
6867
}
6968

0 commit comments

Comments
 (0)