diff --git a/src/com/halo/http/utils/HttpClientTemplate.java b/src/com/halo/http/utils/HttpClientTemplate.java index e44cab9..8abd976 100644 --- a/src/com/halo/http/utils/HttpClientTemplate.java +++ b/src/com/halo/http/utils/HttpClientTemplate.java @@ -51,6 +51,11 @@ private String getUrlWithArgs(String url, Map args) throws HttpU private File saveStreamToFile(InputStream stream) throws HttpUtilsException { UUID uuid = new UUID(ThreadLocalRandom.current().nextLong(), ThreadLocalRandom.current().nextLong()); File downloadFile = new File(uuid.toString() + ".dld"); + try { + downloadFile.createNewFile(); + } catch (IOException e) { + throw new HttpUtilsException("Can't create new download file. ", e); + } OutputStream outputStream = null; try { @@ -68,8 +73,10 @@ private File saveStreamToFile(InputStream stream) throws HttpUtilsException { throw new HttpUtilsException("Can't read from input stream or write to output stream.", e); } finally { try { - outputStream.flush(); - outputStream.close(); + if (null != outputStream) { + outputStream.flush(); + outputStream.close(); + } } catch (IOException e) { throw new HttpUtilsException("Close stream error.", e); } @@ -93,7 +100,7 @@ public String handleResponse(final HttpResponse response) throws ClientProtocolE int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); - return entity != null ? EntityUtils.toString(entity) : null; + return entity != null ? EntityUtils.toString(entity, "UTF-8") : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } @@ -163,7 +170,8 @@ public File handleResponse(final HttpResponse response) throws ClientProtocolExc } @Override - public String post(String url, Map args, String requestBody, String contentType) throws HttpUtilsException { + public String post(String url, Map args, String requestBody, String contentType) + throws HttpUtilsException { String urlWithArgs = getUrlWithArgs(url, args); CloseableHttpClient httpClient = HttpClients.createDefault(); @@ -184,7 +192,7 @@ public String handleResponse(final HttpResponse response) throws ClientProtocolE int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); - return entity != null ? EntityUtils.toString(entity) : null; + return entity != null ? EntityUtils.toString(entity, "UTF-8") : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } @@ -208,7 +216,8 @@ public String handleResponse(final HttpResponse response) throws ClientProtocolE } @Override - public File downloadUsePost(String url, Map args, String requestBody, String contentType) throws HttpUtilsException { + public File downloadUsePost(String url, Map args, String requestBody, String contentType) + throws HttpUtilsException { String urlWithArgs = getUrlWithArgs(url, args); CloseableHttpClient httpClient = HttpClients.createDefault(); diff --git a/src/com/halo/json/utils/JSONUtils.java b/src/com/halo/json/utils/JSONUtils.java index 3874370..4c1d21a 100644 --- a/src/com/halo/json/utils/JSONUtils.java +++ b/src/com/halo/json/utils/JSONUtils.java @@ -1,5 +1,7 @@ package com.halo.json.utils; +import java.util.Map; + import net.sf.json.JSONObject; public class JSONUtils { @@ -16,6 +18,12 @@ public T getJsonBean(String jsonStr) { return (T) JSONObject.toBean(jsonObj, this.clazz); } + @SuppressWarnings({ "rawtypes", "unchecked" }) + public T getComplexJsonBean(String jsonStr, Map classMap) { + JSONObject jsonObj = JSONObject.fromObject(jsonStr); + return (T) JSONObject.toBean(jsonObj, this.clazz, classMap); + } + public String getJsonStr(T jsonBean) { JSONObject jsonObj = JSONObject.fromObject(jsonBean); return jsonObj.toString(); diff --git a/src/com/halo/wechat/capabilities/AbstractCapability.java b/src/com/halo/wechat/capabilities/AbstractCapability.java index ff8c43f..398f2d2 100644 --- a/src/com/halo/wechat/capabilities/AbstractCapability.java +++ b/src/com/halo/wechat/capabilities/AbstractCapability.java @@ -1,5 +1,6 @@ package com.halo.wechat.capabilities; +import java.util.Map; import java.util.Properties; import com.halo.http.utils.HttpTemplate; @@ -96,6 +97,27 @@ public T getJsonBean(JSONUtils jsonUtils, String jsonStr) { return jsonUtils.getJsonBean(jsonStr); } + /** + * 将json字符串转换成复杂对象,即对象的属性有类似List、Map、ArrayList或自定义的类型 + * + * @param jsonUtils + * JSONUtils对象,json字符串转换工具。JSONUtils由泛型定义,可以指定要解析的类。 + * @param jsonStr + * 要解析的json字符串 + * @param classMap + * Map类型,对象中的复杂类型属性列表,String是属性名,Class是属性的类型。例如: + *
+ * 如果要转换的对象中有一个List items属性,这样定义Map
+ * Map classMap = new HashMap(); + *
+ * classMap.put("items", Item.class); + * @return 解析后的对象,类型由JSONUtils的泛型指定 + */ + @SuppressWarnings("rawtypes") + public T getComplexJsonBean(JSONUtils jsonUtils, String jsonStr, Map classMap) { + return jsonUtils.getComplexJsonBean(jsonStr, classMap); + } + /** * 将一个对象编码成json字符串,本方法多用于编码微信能力接口中的请求数据,例如自定义菜单能力接口(CustomMenuCapability) * 中要创建的菜单 diff --git a/src/com/halo/wechat/capabilities/MaterialCapability.java b/src/com/halo/wechat/capabilities/MaterialCapability.java index daf1a20..29d08e7 100644 --- a/src/com/halo/wechat/capabilities/MaterialCapability.java +++ b/src/com/halo/wechat/capabilities/MaterialCapability.java @@ -10,9 +10,12 @@ import com.halo.http.utils.HttpUtilsException; import com.halo.json.utils.JSONUtils; import com.halo.wechat.capabilities.abilities.MaterialAbility; +import com.halo.wechat.capabilities.beans.Content; +import com.halo.wechat.capabilities.beans.Item; import com.halo.wechat.capabilities.beans.MaterialCountBean; import com.halo.wechat.capabilities.beans.MaterialListBean; import com.halo.wechat.capabilities.beans.MaterialResultBean; +import com.halo.wechat.capabilities.beans.NewsItem; /** * 公众号经常有需要用到一些临时性的多媒体素材的场景,例如在使用接口特别是发送消息时,对多媒体文件、多媒体消息的获取和调用等操作, @@ -168,7 +171,8 @@ public MaterialCountBean getMaterialCount() throws CapabilityException { * * @param type * 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)。
- * 本接口中定义了这几种类型的常量“MATERIAL_TYPE_IMAGE”、“MATERIAL_TYPE_VIDEO”、
+ * 本接口中定义了这几种类型的常量“MATERIAL_TYPE_IMAGE”、“MATERIAL_TYPE_VIDEO”、 + *
* “MATERIAL_TYPE_VOICE”、“MATERIAL_TYPE_NEWS” * @param offset * 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回 @@ -197,8 +201,12 @@ public MaterialListBean batchGetMaterial(String type, short offset, short count) } catch (HttpUtilsException e) { throw new CapabilityException("Batch get material failed. ", e); } - - return getJsonBean(new JSONUtils(MaterialListBean.class), resultStr); + @SuppressWarnings("rawtypes") + Map classMap = new HashMap(); + classMap.put("news_item", NewsItem.class); + classMap.put("content", Content.class); + classMap.put("item", Item.class); + return getComplexJsonBean(new JSONUtils(MaterialListBean.class), resultStr, classMap); } /** @@ -265,7 +273,10 @@ public MaterialResultBean getMaterial(String mediaId) throws CapabilityException throw new CapabilityException("Get permanent material failed. ", e); } - return getJsonBean(new JSONUtils(MaterialResultBean.class), resultStr); + @SuppressWarnings("rawtypes") + Map classMap = new HashMap(); + classMap.put("news_item", NewsItem.class); + return getComplexJsonBean(new JSONUtils(MaterialResultBean.class), resultStr, classMap); } /** diff --git a/src/com/halo/wechat/capabilities/beans/Content.java b/src/com/halo/wechat/capabilities/beans/Content.java index 863aa13..198631a 100644 --- a/src/com/halo/wechat/capabilities/beans/Content.java +++ b/src/com/halo/wechat/capabilities/beans/Content.java @@ -33,4 +33,8 @@ public void setNews_item(List news_item) { this.news_item = news_item; } + public Content() { + + } + } diff --git a/src/com/halo/wechat/capabilities/beans/Item.java b/src/com/halo/wechat/capabilities/beans/Item.java new file mode 100644 index 0000000..e96c818 --- /dev/null +++ b/src/com/halo/wechat/capabilities/beans/Item.java @@ -0,0 +1,94 @@ +package com.halo.wechat.capabilities.beans; + +public class Item { + + private String media_id; + + private String name; + + private Content content; + + private String update_time; + + private String url; + + /** + * @return 素材id(必须是永久mediaID) + */ + public String getMedia_id() { + return media_id; + } + + /** + * @param media_id + * 素材id(必须是永久mediaID) + */ + public void setMedia_id(String media_id) { + this.media_id = media_id; + } + + /** + * @return 文件名称 + */ + public String getName() { + return name; + } + + /** + * @param name + * 文件名称 + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return 图文消息内容 + */ + public Content getContent() { + return content; + } + + /** + * @param content + * 图文消息内容 + */ + public void setContent(Content content) { + this.content = content; + } + + /** + * @return 图文消息素材的最后更新时间 + */ + public String getUpdate_time() { + return update_time; + } + + /** + * @param update_time + * 图文消息素材的最后更新时间 + */ + public void setUpdate_time(String update_time) { + this.update_time = update_time; + } + + /** + * @return 当获取的列表是图片素材列表时,该字段是图片的URL + */ + public String getUrl() { + return url; + } + + /** + * @param url + * 当获取的列表是图片素材列表时,该字段是图片的URL + */ + public void setUrl(String url) { + this.url = url; + } + + public Item() { + + } + +} diff --git a/src/com/halo/wechat/capabilities/beans/MaterialListBean.java b/src/com/halo/wechat/capabilities/beans/MaterialListBean.java index 6a884e8..837302d 100644 --- a/src/com/halo/wechat/capabilities/beans/MaterialListBean.java +++ b/src/com/halo/wechat/capabilities/beans/MaterialListBean.java @@ -16,95 +16,6 @@ */ public class MaterialListBean extends ResultBean { - public class Item { - - private String media_id; - - private String name; - - private Content content; - - private String update_time; - - private String url; - - /** - * @return 素材id(必须是永久mediaID) - */ - public String getMedia_id() { - return media_id; - } - - /** - * @param media_id - * 素材id(必须是永久mediaID) - */ - public void setMedia_id(String media_id) { - this.media_id = media_id; - } - - /** - * @return 文件名称 - */ - public String getName() { - return name; - } - - /** - * @param name - * 文件名称 - */ - public void setName(String name) { - this.name = name; - } - - /** - * @return 图文消息内容 - */ - public Content getContent() { - return content; - } - - /** - * @param content - * 图文消息内容 - */ - public void setContent(Content content) { - this.content = content; - } - - /** - * @return 图文消息素材的最后更新时间 - */ - public String getUpdate_time() { - return update_time; - } - - /** - * @param update_time - * 图文消息素材的最后更新时间 - */ - public void setUpdate_time(String update_time) { - this.update_time = update_time; - } - - /** - * @return 当获取的列表是图片素材列表时,该字段是图片的URL - */ - public String getUrl() { - return url; - } - - /** - * @param url - * 当获取的列表是图片素材列表时,该字段是图片的URL - */ - public void setUrl(String url) { - this.url = url; - } - - } - private int total_count; private int item_count; diff --git a/src/com/halo/wechat/capabilities/beans/NewsItem.java b/src/com/halo/wechat/capabilities/beans/NewsItem.java index 323cddd..839379a 100644 --- a/src/com/halo/wechat/capabilities/beans/NewsItem.java +++ b/src/com/halo/wechat/capabilities/beans/NewsItem.java @@ -150,4 +150,8 @@ public void setContent_source_url(String content_source_url) { this.content_source_url = content_source_url; } + public NewsItem() { + + } + } diff --git a/src/test/MaterialCapabilityTest.java b/src/test/MaterialCapabilityTest.java index b7a0481..babd8ea 100644 --- a/src/test/MaterialCapabilityTest.java +++ b/src/test/MaterialCapabilityTest.java @@ -18,6 +18,7 @@ import com.halo.spring.utils.SpringUtils; import com.halo.wechat.capabilities.CapabilityException; import com.halo.wechat.capabilities.abilities.MaterialAbility; +import com.halo.wechat.capabilities.beans.Item; import com.halo.wechat.capabilities.beans.MaterialCountBean; import com.halo.wechat.capabilities.beans.MaterialListBean; import com.halo.wechat.capabilities.beans.NewsItem; @@ -83,7 +84,7 @@ public void testGetMaterial() throws Exception { assertNotNull(materialListBean); String savedRecord = "共下载" + String.valueOf(count) + "条素材\r\n"; - for (MaterialListBean.Item item : materialListBean.getItem()) { + for (Item item : materialListBean.getItem()) { for (NewsItem newsItem : item.getContent().getNews_item()) { File bigPicFile; try {