Skip to content

Commit

Permalink
修正了素材下载的多处bug。并且,我决定用中文写注释了(换句话说:我决定不装逼了)
Browse files Browse the repository at this point in the history
  • Loading branch information
junior9919 committed Nov 17, 2015
1 parent 7080683 commit 42ec6e5
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 100 deletions.
21 changes: 15 additions & 6 deletions src/com/halo/http/utils/HttpClientTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ private String getUrlWithArgs(String url, Map<String, String> 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 {
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -163,7 +170,8 @@ public File handleResponse(final HttpResponse response) throws ClientProtocolExc
}

@Override
public String post(String url, Map<String, String> args, String requestBody, String contentType) throws HttpUtilsException {
public String post(String url, Map<String, String> args, String requestBody, String contentType)
throws HttpUtilsException {
String urlWithArgs = getUrlWithArgs(url, args);

CloseableHttpClient httpClient = HttpClients.createDefault();
Expand All @@ -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);
}
Expand All @@ -208,7 +216,8 @@ public String handleResponse(final HttpResponse response) throws ClientProtocolE
}

@Override
public File downloadUsePost(String url, Map<String, String> args, String requestBody, String contentType) throws HttpUtilsException {
public File downloadUsePost(String url, Map<String, String> args, String requestBody, String contentType)
throws HttpUtilsException {
String urlWithArgs = getUrlWithArgs(url, args);

CloseableHttpClient httpClient = HttpClients.createDefault();
Expand Down
8 changes: 8 additions & 0 deletions src/com/halo/json/utils/JSONUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.halo.json.utils;

import java.util.Map;

import net.sf.json.JSONObject;

public class JSONUtils<T> {
Expand All @@ -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<String, Class> 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();
Expand Down
22 changes: 22 additions & 0 deletions src/com/halo/wechat/capabilities/AbstractCapability.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.halo.wechat.capabilities;

import java.util.Map;
import java.util.Properties;

import com.halo.http.utils.HttpTemplate;
Expand Down Expand Up @@ -96,6 +97,27 @@ public <T> T getJsonBean(JSONUtils<T> jsonUtils, String jsonStr) {
return jsonUtils.getJsonBean(jsonStr);
}

/**
* 将json字符串转换成复杂对象,即对象的属性有类似List、Map、ArrayList或自定义的类型
*
* @param jsonUtils
* JSONUtils对象,json字符串转换工具。JSONUtils由泛型定义,可以指定要解析的类。
* @param jsonStr
* 要解析的json字符串
* @param classMap
* Map<String, Class>类型,对象中的复杂类型属性列表,String是属性名,Class是属性的类型。例如:
* <br>
* 如果要转换的对象中有一个List<Item> items属性,这样定义Map<br>
* Map<String, Class> classMap = new HashMap<String, Class>();
* <br>
* classMap.put("items", Item.class);
* @return 解析后的对象,类型由JSONUtils的泛型指定
*/
@SuppressWarnings("rawtypes")
public <T> T getComplexJsonBean(JSONUtils<T> jsonUtils, String jsonStr, Map<String, Class> classMap) {
return jsonUtils.getComplexJsonBean(jsonStr, classMap);
}

/**
* 将一个对象编码成json字符串,本方法多用于编码微信能力接口中的请求数据,例如自定义菜单能力接口(CustomMenuCapability)
* 中要创建的菜单
Expand Down
19 changes: 15 additions & 4 deletions src/com/halo/wechat/capabilities/MaterialCapability.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* 公众号经常有需要用到一些临时性的多媒体素材的场景,例如在使用接口特别是发送消息时,对多媒体文件、多媒体消息的获取和调用等操作,
Expand Down Expand Up @@ -168,7 +171,8 @@ public MaterialCountBean getMaterialCount() throws CapabilityException {
*
* @param type
* 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)。<br>
* 本接口中定义了这几种类型的常量“MATERIAL_TYPE_IMAGE”、“MATERIAL_TYPE_VIDEO”、<br>
* 本接口中定义了这几种类型的常量“MATERIAL_TYPE_IMAGE”、“MATERIAL_TYPE_VIDEO”、
* <br>
* “MATERIAL_TYPE_VOICE”、“MATERIAL_TYPE_NEWS”
* @param offset
* 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
Expand Down Expand Up @@ -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>(MaterialListBean.class), resultStr);
@SuppressWarnings("rawtypes")
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("news_item", NewsItem.class);
classMap.put("content", Content.class);
classMap.put("item", Item.class);
return getComplexJsonBean(new JSONUtils<MaterialListBean>(MaterialListBean.class), resultStr, classMap);
}

/**
Expand Down Expand Up @@ -265,7 +273,10 @@ public MaterialResultBean getMaterial(String mediaId) throws CapabilityException
throw new CapabilityException("Get permanent material failed. ", e);
}

return getJsonBean(new JSONUtils<MaterialResultBean>(MaterialResultBean.class), resultStr);
@SuppressWarnings("rawtypes")
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("news_item", NewsItem.class);
return getComplexJsonBean(new JSONUtils<MaterialResultBean>(MaterialResultBean.class), resultStr, classMap);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/com/halo/wechat/capabilities/beans/Content.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public void setNews_item(List<NewsItem> news_item) {
this.news_item = news_item;
}

public Content() {

}

}
94 changes: 94 additions & 0 deletions src/com/halo/wechat/capabilities/beans/Item.java
Original file line number Diff line number Diff line change
@@ -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() {

}

}
89 changes: 0 additions & 89 deletions src/com/halo/wechat/capabilities/beans/MaterialListBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/com/halo/wechat/capabilities/beans/NewsItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,8 @@ public void setContent_source_url(String content_source_url) {
this.content_source_url = content_source_url;
}

public NewsItem() {

}

}
3 changes: 2 additions & 1 deletion src/test/MaterialCapabilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 42ec6e5

Please sign in to comment.