Skip to content

Commit

Permalink
使用java1.7新语法,改进序列化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
codepiano committed Jul 29, 2015
1 parent a00d693 commit 4da11fc
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.chanjar.weixin.mp.api;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand All @@ -24,6 +23,7 @@
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.*;
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.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
Expand Down Expand Up @@ -275,11 +275,11 @@ public WxMpMaterialCountResult materialCount() throws WxErrorException {

public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException {
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("offset", offset);
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);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class);
Expand All @@ -290,11 +290,11 @@ public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count

public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException {
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("offset", offset);
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);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
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.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
Expand All @@ -31,9 +31,9 @@ public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, Strin
httpPost.setConfig(config);
}

Map<String, String> params = new HashMap<String, String>();
Map<String, String> params = new HashMap<>();
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);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
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.util.json.WxMpGsonBuilder;
import org.apache.http.HttpHost;
Expand Down Expand Up @@ -32,9 +32,9 @@ public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpPro
httpPost.setConfig(config);
}

Map<String, String> params = new HashMap<String, String>();
Map<String, String> params = new HashMap<>();
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);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
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.result.WxMpMaterialUploadResult;
import org.apache.http.HttpHost;
Expand Down Expand Up @@ -39,7 +39,7 @@ public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost
multipartEntityBuilder.addPart("media", new InputStreamBody(bufferedInputStream, material.getName()));
Map<String, String> form = material.getForm();
if (material.getForm() != null) {
multipartEntityBuilder.addTextBody("description", new Gson().toJson(form));
multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form));
}
httpPost.setEntity(multipartEntityBuilder.build());
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
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 org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
Expand All @@ -31,9 +31,9 @@ public WxMpMaterialVideoInfoResult execute(CloseableHttpClient httpclient, HttpH
httpPost.setConfig(config);
}

Map<String, String> params = new HashMap<String, String>();
Map<String, String> params = new HashMap<>();
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);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.InputStreamResponseHandler;
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.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
Expand Down Expand Up @@ -41,17 +41,17 @@ public InputStream execute(CloseableHttpClient httpclient, HttpHost httpProxy, S
httpPost.setConfig(config);
}

Map<String, String> params = new HashMap<String, String>();
Map<String, String> params = new HashMap<>();
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);
// 下载媒体文件出错
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
byte[] responseContent = IOUtils.toByteArray(inputStream);
String responseContentString = new String(responseContent, "UTF-8");
if (responseContentString.length() < 100) {
try {
WxError wxError = new Gson().fromJson(responseContentString, WxError.class);
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public WxMpMaterialFileBatchGetResult deserialize(JsonElement jsonElement, Type
}
if (json.get("item") != null && !json.get("item").isJsonNull()) {
JsonArray item = json.getAsJsonArray("item");
List<WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem> items = new ArrayList<WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem>();
List<WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem> items = new ArrayList<>();
for (JsonElement anItem : item) {
JsonObject articleInfo = anItem.getAsJsonObject();
items.add(WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public WxMpMaterialNewsBatchGetResult deserialize(JsonElement jsonElement, Type
}
if (json.get("item") != null && !json.get("item").isJsonNull()) {
JsonArray item = json.getAsJsonArray("item");
List<WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem> items = new ArrayList<WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem>();
List<WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem> items = new ArrayList<>();
for (JsonElement anItem : item) {
JsonObject articleInfo = anItem.getAsJsonObject();
items.add(WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class WxMpMaterialAPITest {
@Inject
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,测试上传图文使用
private String thumbMediaId = "";
// 单图文消息media_id
Expand Down Expand Up @@ -64,7 +64,7 @@ public void testUploadMaterial(String mediaType, String fileType, String fileNam
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("length", tempFile.length());
materialInfo.put("filename", tempFile.getName());
Expand Down Expand Up @@ -217,18 +217,18 @@ public void testDeleteMaterial(String mediaId) throws WxErrorException {
public Object[][] downloadMaterial() {
Object[][] params = new Object[this.media_ids.size()][];
int index = 0;
for (Iterator<String> iterator = this.media_ids.keySet().iterator(); iterator.hasNext(); ) {
params[index] = new Object[]{iterator.next()};
for (String mediaId : this.media_ids.keySet()) {
params[index] = new Object[]{mediaId};
index++;
}
return params;
}

@DataProvider
public Iterator<Object[]> allTestMaterial() {
List<Object[]> params = new ArrayList<Object[]>();
for (Iterator<String> iterator = this.media_ids.keySet().iterator(); iterator.hasNext(); ) {
params.add(new Object[]{iterator.next()});
List<Object[]> params = new ArrayList<>();
for (String mediaId : this.media_ids.keySet()) {
params.add(new Object[]{mediaId});
}
params.add(new Object[]{this.singleNewsMediaId});
params.add(new Object[]{this.multiNewsMediaId});
Expand Down

0 comments on commit 4da11fc

Please sign in to comment.