mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 18:46:10 +08:00 
			
		
		
		
	使用java1.7新语法,改进序列化代码
This commit is contained in:
		| @ -1,6 +1,5 @@ | |||||||
| package me.chanjar.weixin.mp.api; | package me.chanjar.weixin.mp.api; | ||||||
|  |  | ||||||
| import com.google.gson.Gson; |  | ||||||
| import com.google.gson.JsonArray; | import com.google.gson.JsonArray; | ||||||
| import com.google.gson.JsonElement; | import com.google.gson.JsonElement; | ||||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||||
| @ -24,6 +23,7 @@ import me.chanjar.weixin.common.util.crypto.WxCryptUtil; | |||||||
| import me.chanjar.weixin.common.util.fs.FileUtils; | import me.chanjar.weixin.common.util.fs.FileUtils; | ||||||
| import me.chanjar.weixin.common.util.http.*; | import me.chanjar.weixin.common.util.http.*; | ||||||
| import me.chanjar.weixin.common.util.json.GsonHelper; | import me.chanjar.weixin.common.util.json.GsonHelper; | ||||||
|  | import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||||
| import me.chanjar.weixin.common.util.xml.XStreamInitializer; | import me.chanjar.weixin.common.util.xml.XStreamInitializer; | ||||||
| import me.chanjar.weixin.mp.bean.*; | import me.chanjar.weixin.mp.bean.*; | ||||||
| import me.chanjar.weixin.mp.bean.result.*; | import me.chanjar.weixin.mp.bean.result.*; | ||||||
| @ -275,11 +275,11 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|  |  | ||||||
|   public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException { |   public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException { | ||||||
|     String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"; |     String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"; | ||||||
|     Map<String, Object> params = new HashMap<String, Object>(); |     Map<String, Object> params = new HashMap<>(); | ||||||
|     params.put("type", WxConsts.MATERIAL_NEWS); |     params.put("type", WxConsts.MATERIAL_NEWS); | ||||||
|     params.put("offset", offset); |     params.put("offset", offset); | ||||||
|     params.put("count", count); |     params.put("count", count); | ||||||
|     String responseText = post(url, new Gson().toJson(params)); |     String responseText = post(url, WxGsonBuilder.create().toJson(params)); | ||||||
|     WxError wxError = WxError.fromJson(responseText); |     WxError wxError = WxError.fromJson(responseText); | ||||||
|     if (wxError.getErrorCode() == 0) { |     if (wxError.getErrorCode() == 0) { | ||||||
|       return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class); |       return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class); | ||||||
| @ -290,11 +290,11 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|  |  | ||||||
|   public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException { |   public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException { | ||||||
|     String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"; |     String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"; | ||||||
|     Map<String, Object> params = new HashMap<String, Object>(); |     Map<String, Object> params = new HashMap<>(); | ||||||
|     params.put("type", type); |     params.put("type", type); | ||||||
|     params.put("offset", offset); |     params.put("offset", offset); | ||||||
|     params.put("count", count); |     params.put("count", count); | ||||||
|     String responseText = post(url, new Gson().toJson(params)); |     String responseText = post(url, WxGsonBuilder.create().toJson(params)); | ||||||
|     WxError wxError = WxError.fromJson(responseText); |     WxError wxError = WxError.fromJson(responseText); | ||||||
|     if (wxError.getErrorCode() == 0) { |     if (wxError.getErrorCode() == 0) { | ||||||
|       return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class); |       return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class); | ||||||
|  | |||||||
| @ -1,10 +1,10 @@ | |||||||
| package me.chanjar.weixin.mp.util.http; | package me.chanjar.weixin.mp.util.http; | ||||||
|  |  | ||||||
| import com.google.gson.Gson; |  | ||||||
| import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||||
| import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | import me.chanjar.weixin.common.util.http.RequestExecutor; | ||||||
| import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | ||||||
|  | import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||||
| import org.apache.http.HttpHost; | import org.apache.http.HttpHost; | ||||||
| import org.apache.http.client.ClientProtocolException; | import org.apache.http.client.ClientProtocolException; | ||||||
| import org.apache.http.client.config.RequestConfig; | import org.apache.http.client.config.RequestConfig; | ||||||
| @ -31,9 +31,9 @@ public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, S | |||||||
|       httpPost.setConfig(config); |       httpPost.setConfig(config); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     Map<String, String> params = new HashMap<String, String>(); |     Map<String, String> params = new HashMap<>(); | ||||||
|     params.put("media_id", materialId); |     params.put("media_id", materialId); | ||||||
|     httpPost.setEntity(new StringEntity(new Gson().toJson(params))); |     httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | ||||||
|     CloseableHttpResponse response = httpclient.execute(httpPost); |     CloseableHttpResponse response = httpclient.execute(httpPost); | ||||||
|     String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); |     String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | ||||||
|     WxError error = WxError.fromJson(responseContent); |     WxError error = WxError.fromJson(responseContent); | ||||||
|  | |||||||
| @ -1,10 +1,10 @@ | |||||||
| package me.chanjar.weixin.mp.util.http; | package me.chanjar.weixin.mp.util.http; | ||||||
|  |  | ||||||
| import com.google.gson.Gson; |  | ||||||
| import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||||
| import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | import me.chanjar.weixin.common.util.http.RequestExecutor; | ||||||
| import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | ||||||
|  | import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||||
| import me.chanjar.weixin.mp.bean.WxMpMaterialNews; | import me.chanjar.weixin.mp.bean.WxMpMaterialNews; | ||||||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | ||||||
| import org.apache.http.HttpHost; | import org.apache.http.HttpHost; | ||||||
| @ -32,9 +32,9 @@ public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMate | |||||||
|       httpPost.setConfig(config); |       httpPost.setConfig(config); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     Map<String, String> params = new HashMap<String, String>(); |     Map<String, String> params = new HashMap<>(); | ||||||
|     params.put("media_id", materialId); |     params.put("media_id", materialId); | ||||||
|     httpPost.setEntity(new StringEntity(new Gson().toJson(params))); |     httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | ||||||
|     CloseableHttpResponse response = httpclient.execute(httpPost); |     CloseableHttpResponse response = httpclient.execute(httpPost); | ||||||
|     String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); |     String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | ||||||
|     WxError error = WxError.fromJson(responseContent); |     WxError error = WxError.fromJson(responseContent); | ||||||
|  | |||||||
| @ -1,10 +1,10 @@ | |||||||
| package me.chanjar.weixin.mp.util.http; | package me.chanjar.weixin.mp.util.http; | ||||||
|  |  | ||||||
| import com.google.gson.Gson; |  | ||||||
| import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||||
| import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | import me.chanjar.weixin.common.util.http.RequestExecutor; | ||||||
| import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | ||||||
|  | import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||||
| import me.chanjar.weixin.mp.bean.WxMpMaterial; | import me.chanjar.weixin.mp.bean.WxMpMaterial; | ||||||
| import me.chanjar.weixin.mp.bean.result.WxMpMaterialUploadResult; | import me.chanjar.weixin.mp.bean.result.WxMpMaterialUploadResult; | ||||||
| import org.apache.http.HttpHost; | import org.apache.http.HttpHost; | ||||||
| @ -39,7 +39,7 @@ public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMateri | |||||||
|       multipartEntityBuilder.addPart("media", new InputStreamBody(bufferedInputStream, material.getName())); |       multipartEntityBuilder.addPart("media", new InputStreamBody(bufferedInputStream, material.getName())); | ||||||
|       Map<String, String> form = material.getForm(); |       Map<String, String> form = material.getForm(); | ||||||
|       if (material.getForm() != null) { |       if (material.getForm() != null) { | ||||||
|         multipartEntityBuilder.addTextBody("description", new Gson().toJson(form)); |         multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form)); | ||||||
|       } |       } | ||||||
|       httpPost.setEntity(multipartEntityBuilder.build()); |       httpPost.setEntity(multipartEntityBuilder.build()); | ||||||
|       httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); |       httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | ||||||
|  | |||||||
| @ -1,10 +1,10 @@ | |||||||
| package me.chanjar.weixin.mp.util.http; | package me.chanjar.weixin.mp.util.http; | ||||||
|  |  | ||||||
| import com.google.gson.Gson; |  | ||||||
| import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||||
| import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | import me.chanjar.weixin.common.util.http.RequestExecutor; | ||||||
| import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | ||||||
|  | import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||||
| import me.chanjar.weixin.mp.bean.result.WxMpMaterialVideoInfoResult; | import me.chanjar.weixin.mp.bean.result.WxMpMaterialVideoInfoResult; | ||||||
| import org.apache.http.HttpHost; | import org.apache.http.HttpHost; | ||||||
| import org.apache.http.client.ClientProtocolException; | import org.apache.http.client.ClientProtocolException; | ||||||
| @ -31,9 +31,9 @@ public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMat | |||||||
|       httpPost.setConfig(config); |       httpPost.setConfig(config); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     Map<String, String> params = new HashMap<String, String>(); |     Map<String, String> params = new HashMap<>(); | ||||||
|     params.put("media_id", materialId); |     params.put("media_id", materialId); | ||||||
|     httpPost.setEntity(new StringEntity(new Gson().toJson(params))); |     httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | ||||||
|     CloseableHttpResponse response = httpclient.execute(httpPost); |     CloseableHttpResponse response = httpclient.execute(httpPost); | ||||||
|     String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); |     String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | ||||||
|     WxError error = WxError.fromJson(responseContent); |     WxError error = WxError.fromJson(responseContent); | ||||||
|  | |||||||
| @ -1,10 +1,10 @@ | |||||||
| package me.chanjar.weixin.mp.util.http; | package me.chanjar.weixin.mp.util.http; | ||||||
|  |  | ||||||
| import com.google.gson.Gson; |  | ||||||
| import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||||
| import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||||
| import me.chanjar.weixin.common.util.http.InputStreamResponseHandler; | import me.chanjar.weixin.common.util.http.InputStreamResponseHandler; | ||||||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | import me.chanjar.weixin.common.util.http.RequestExecutor; | ||||||
|  | import me.chanjar.weixin.common.util.json.WxGsonBuilder; | ||||||
| import org.apache.commons.io.IOUtils; | import org.apache.commons.io.IOUtils; | ||||||
| import org.apache.http.HttpHost; | import org.apache.http.HttpHost; | ||||||
| import org.apache.http.client.ClientProtocolException; | import org.apache.http.client.ClientProtocolException; | ||||||
| @ -41,9 +41,9 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec | |||||||
|       httpPost.setConfig(config); |       httpPost.setConfig(config); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     Map<String, String> params = new HashMap<String, String>(); |     Map<String, String> params = new HashMap<>(); | ||||||
|     params.put("media_id", materialId); |     params.put("media_id", materialId); | ||||||
|     httpPost.setEntity(new StringEntity(new Gson().toJson(params))); |     httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); | ||||||
|     CloseableHttpResponse response = httpclient.execute(httpPost); |     CloseableHttpResponse response = httpclient.execute(httpPost); | ||||||
|     // 下载媒体文件出错 |     // 下载媒体文件出错 | ||||||
|     InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); |     InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); | ||||||
| @ -51,7 +51,7 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec | |||||||
|     String responseContentString = new String(responseContent, "UTF-8"); |     String responseContentString = new String(responseContent, "UTF-8"); | ||||||
|     if (responseContentString.length() < 100) { |     if (responseContentString.length() < 100) { | ||||||
|       try { |       try { | ||||||
|         WxError wxError = new Gson().fromJson(responseContentString, WxError.class); |         WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class); | ||||||
|         if (wxError.getErrorCode() != 0) { |         if (wxError.getErrorCode() != 0) { | ||||||
|           throw new WxErrorException(wxError); |           throw new WxErrorException(wxError); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -29,7 +29,7 @@ public class WxMpMaterialFileBatchGetGsonAdapter implements JsonDeserializer<WxM | |||||||
|     } |     } | ||||||
|     if (json.get("item") != null && !json.get("item").isJsonNull()) { |     if (json.get("item") != null && !json.get("item").isJsonNull()) { | ||||||
|       JsonArray item = json.getAsJsonArray("item"); |       JsonArray item = json.getAsJsonArray("item"); | ||||||
|       List<WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem> items = new ArrayList<WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem>(); |       List<WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem> items = new ArrayList<>(); | ||||||
|       for (JsonElement anItem : item) { |       for (JsonElement anItem : item) { | ||||||
|         JsonObject articleInfo = anItem.getAsJsonObject(); |         JsonObject articleInfo = anItem.getAsJsonObject(); | ||||||
|         items.add(WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class)); |         items.add(WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class)); | ||||||
|  | |||||||
| @ -29,7 +29,7 @@ public class WxMpMaterialNewsBatchGetGsonAdapter implements JsonDeserializer<WxM | |||||||
|     } |     } | ||||||
|     if (json.get("item") != null && !json.get("item").isJsonNull()) { |     if (json.get("item") != null && !json.get("item").isJsonNull()) { | ||||||
|       JsonArray item = json.getAsJsonArray("item"); |       JsonArray item = json.getAsJsonArray("item"); | ||||||
|       List<WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem> items = new ArrayList<WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem>(); |       List<WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem> items = new ArrayList<>(); | ||||||
|       for (JsonElement anItem : item) { |       for (JsonElement anItem : item) { | ||||||
|         JsonObject articleInfo = anItem.getAsJsonObject(); |         JsonObject articleInfo = anItem.getAsJsonObject(); | ||||||
|         items.add(WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem.class)); |         items.add(WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem.class)); | ||||||
|  | |||||||
| @ -31,7 +31,7 @@ public class WxMpMaterialAPITest { | |||||||
|   @Inject |   @Inject | ||||||
|   protected WxMpServiceImpl wxService; |   protected WxMpServiceImpl wxService; | ||||||
|  |  | ||||||
|   private Map<String, Map<String, Object>> media_ids = new LinkedHashMap<String, Map<String, Object>>(); |   private Map<String, Map<String, Object>> media_ids = new LinkedHashMap<>(); | ||||||
|   // 缩略图的id,测试上传图文使用 |   // 缩略图的id,测试上传图文使用 | ||||||
|   private String thumbMediaId = ""; |   private String thumbMediaId = ""; | ||||||
|   // 单图文消息media_id |   // 单图文消息media_id | ||||||
| @ -64,7 +64,7 @@ public class WxMpMaterialAPITest { | |||||||
|       thumbMediaId = res.getMediaId(); |       thumbMediaId = res.getMediaId(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     Map<String, Object> materialInfo = new HashMap<String, Object>(); |     Map<String, Object> materialInfo = new HashMap<>(); | ||||||
|     materialInfo.put("media_id", res.getMediaId()); |     materialInfo.put("media_id", res.getMediaId()); | ||||||
|     materialInfo.put("length", tempFile.length()); |     materialInfo.put("length", tempFile.length()); | ||||||
|     materialInfo.put("filename", tempFile.getName()); |     materialInfo.put("filename", tempFile.getName()); | ||||||
| @ -217,8 +217,8 @@ public class WxMpMaterialAPITest { | |||||||
|   public Object[][] downloadMaterial() { |   public Object[][] downloadMaterial() { | ||||||
|     Object[][] params = new Object[this.media_ids.size()][]; |     Object[][] params = new Object[this.media_ids.size()][]; | ||||||
|     int index = 0; |     int index = 0; | ||||||
|     for (Iterator<String> iterator = this.media_ids.keySet().iterator(); iterator.hasNext(); ) { |     for (String mediaId : this.media_ids.keySet()) { | ||||||
|       params[index] = new Object[]{iterator.next()}; |       params[index] = new Object[]{mediaId}; | ||||||
|       index++; |       index++; | ||||||
|     } |     } | ||||||
|     return params; |     return params; | ||||||
| @ -226,9 +226,9 @@ public class WxMpMaterialAPITest { | |||||||
|  |  | ||||||
|   @DataProvider |   @DataProvider | ||||||
|   public Iterator<Object[]> allTestMaterial() { |   public Iterator<Object[]> allTestMaterial() { | ||||||
|     List<Object[]> params = new ArrayList<Object[]>(); |     List<Object[]> params = new ArrayList<>(); | ||||||
|     for (Iterator<String> iterator = this.media_ids.keySet().iterator(); iterator.hasNext(); ) { |     for (String mediaId : this.media_ids.keySet()) { | ||||||
|       params.add(new Object[]{iterator.next()}); |       params.add(new Object[]{mediaId}); | ||||||
|     } |     } | ||||||
|     params.add(new Object[]{this.singleNewsMediaId}); |     params.add(new Object[]{this.singleNewsMediaId}); | ||||||
|     params.add(new Object[]{this.multiNewsMediaId}); |     params.add(new Object[]{this.multiNewsMediaId}); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 codepiano
					codepiano