瀏覽代碼

Merge pull request #184 from codepiano/develop

使用java1.7新语法,改进序列化代码
master
Daniel Qian 9 年之前
父節點
當前提交
0acae5783a
共有 9 個文件被更改,包括 29 次插入29 次删除
  1. +5
    -5
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java
  2. +3
    -3
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialDeleteRequestExecutor.java
  3. +3
    -3
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialNewsInfoRequestExecutor.java
  4. +2
    -2
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java
  5. +3
    -3
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVideoInfoRequestExecutor.java
  6. +4
    -4
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java
  7. +1
    -1
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMaterialFileBatchGetGsonAdapter.java
  8. +1
    -1
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMaterialNewsBatchGetGsonAdapter.java
  9. +7
    -7
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMaterialAPITest.java

+ 5
- 5
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java 查看文件

@@ -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;
@@ -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.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.*;
@@ -275,11 +275,11 @@ public class WxMpServiceImpl implements WxMpService {

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);
@@ -290,11 +290,11 @@ public class WxMpServiceImpl implements WxMpService {

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


+ 3
- 3
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialDeleteRequestExecutor.java 查看文件

@@ -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;
@@ -31,9 +31,9 @@ public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, 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);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);


+ 3
- 3
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialNewsInfoRequestExecutor.java 查看文件

@@ -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;
@@ -32,9 +32,9 @@ public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMate
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);


+ 2
- 2
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java 查看文件

@@ -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;
@@ -39,7 +39,7 @@ public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMateri
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());


+ 3
- 3
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVideoInfoRequestExecutor.java 查看文件

@@ -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;
@@ -31,9 +31,9 @@ public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMat
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);


+ 4
- 4
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java 查看文件

@@ -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;
@@ -41,9 +41,9 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec
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);
@@ -51,7 +51,7 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec
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);
}


+ 1
- 1
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMaterialFileBatchGetGsonAdapter.java 查看文件

@@ -29,7 +29,7 @@ public class WxMpMaterialFileBatchGetGsonAdapter implements JsonDeserializer<WxM
}
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));


+ 1
- 1
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMaterialNewsBatchGetGsonAdapter.java 查看文件

@@ -29,7 +29,7 @@ public class WxMpMaterialNewsBatchGetGsonAdapter implements JsonDeserializer<WxM
}
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));


+ 7
- 7
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMaterialAPITest.java 查看文件

@@ -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
@@ -64,7 +64,7 @@ public class WxMpMaterialAPITest {
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());
@@ -217,8 +217,8 @@ public class WxMpMaterialAPITest {
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;
@@ -226,9 +226,9 @@ public class WxMpMaterialAPITest {

@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});


Loading…
取消
儲存