فهرست منبع

修复部分可能造成resource leak的代码

BinaryWang 8 سال پیش
والد
کامیت
a294bc42f5

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

@@ -1,12 +1,17 @@
 package me.chanjar.weixin.mp.api.impl;
 
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import com.google.gson.JsonPrimitive;
-import com.google.gson.internal.Streams;
 import com.google.gson.reflect.TypeToken;
-import com.google.gson.stream.JsonReader;
+
 import me.chanjar.weixin.common.bean.WxCardApiSignature;
 import me.chanjar.weixin.common.bean.result.WxError;
 import me.chanjar.weixin.common.exception.WxErrorException;
@@ -17,12 +22,6 @@ import me.chanjar.weixin.mp.api.WxMpCardService;
 import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
 import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.StringReader;
-import java.security.NoSuchAlgorithmException;
-import java.util.Arrays;
 
 /**
  * Created by Binary Wang on 2016/7/27.
@@ -76,7 +75,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
         if (this.wxMpService.getWxMpConfigStorage().isCardApiTicketExpired()) {
           String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card";
           String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null);
-          JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+          JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
           JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
           String cardApiTicket = tmpJsonObject.get("ticket").getAsString();
           int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
@@ -136,7 +135,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
     JsonObject param = new JsonObject();
     param.addProperty("encrypt_code", encryptCode);
     String responseContent = this.wxMpService.post(url, param.toString());
-    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+    JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
     JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
     JsonPrimitive jsonPrimitive = tmpJsonObject.getAsJsonPrimitive("code");
     return jsonPrimitive.getAsString();
@@ -158,7 +157,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
     param.addProperty("code", code);
     param.addProperty("check_consume", checkConsume);
     String responseContent = this.wxMpService.post(url, param.toString());
-    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+    JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
     return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
             new TypeToken<WxMpCardResult>() {
             }.getType());
@@ -217,7 +216,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
     param.addProperty("openid", openId);
     param.addProperty("is_mark", isMark);
     String responseContent = this.wxMpService.post(url, param.toString());
-    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+    JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
     WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
             new TypeToken<WxMpCardResult>() { }.getType());
     if (!cardResult.getErrorCode().equals("0")) {

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

@@ -1,10 +1,12 @@
 package me.chanjar.weixin.mp.api.impl;
 
+import java.util.List;
+
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
-import com.google.gson.internal.Streams;
+import com.google.gson.JsonParser;
 import com.google.gson.reflect.TypeToken;
-import com.google.gson.stream.JsonReader;
+
 import me.chanjar.weixin.common.exception.WxErrorException;
 import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
 import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
@@ -14,9 +16,6 @@ import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.bean.WxMpGroup;
 import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
 
-import java.io.StringReader;
-import java.util.List;
-
 /**
  * Created by Binary Wang on 2016/7/21.
  */
@@ -51,7 +50,7 @@ public class WxMpGroupServiceImpl implements WxMpGroupService {
      * 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} }
      * 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] }
      */
-    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+    JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
     return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("groups"),
             new TypeToken<List<WxMpGroup>>() {
             }.getType());
@@ -63,7 +62,7 @@ public class WxMpGroupServiceImpl implements WxMpGroupService {
     JsonObject o = new JsonObject();
     o.addProperty("openid", openid);
     String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, o.toString());
-    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+    JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
     return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid"));
   }