添加多媒体文件下载支持

This commit is contained in:
Daniel Qian
2014-08-24 14:18:31 +08:00
parent 7a2f7a8a4b
commit 71215bbe79
25 changed files with 386 additions and 117 deletions

View File

@@ -0,0 +1,115 @@
/*
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
*
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
* arose from modification of the original source, or other redistribution of this source
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
*/
package chanjarster.weixin.util.json;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class GsonHelper {
public static boolean isNull(JsonElement element) {
return element == null || element.isJsonNull();
}
public static boolean isNotNull(JsonElement element) {
return !isNull(element);
}
public static Long getLong(JsonObject json, String property) {
return getAsLong(json.get(property));
}
public static long getPrimitiveLong(JsonObject json, String property) {
return getAsPrimitiveLong(json.get(property));
}
public static Integer getInteger(JsonObject json, String property) {
return getAsInteger(json.get(property));
}
public static int getPrimitiveInteger(JsonObject json, String property) {
return getAsPrimitiveInt(json.get(property));
}
public static Double getDouble(JsonObject json, String property) {
return getAsDouble(json.get(property));
}
public static double getPrimitiveDouble(JsonObject json, String property) {
return getAsPrimitiveDouble(json.get(property));
}
public static Float getFloat(JsonObject json, String property) {
return getAsFloat(json.get(property));
}
public static float getPrimitiveFloat(JsonObject json, String property) {
return getAsPrimitiveFloat(json.get(property));
}
public static Boolean getBoolean(JsonObject json, String property) {
return getAsBoolean(json.get(property));
}
public static String getString(JsonObject json, String property) {
return getAsString(json.get(property));
}
public static String getAsString(JsonElement element) {
return isNull(element) ? null : element.getAsString();
}
public static Long getAsLong(JsonElement element) {
return isNull(element) ? null : element.getAsLong();
}
public static long getAsPrimitiveLong(JsonElement element) {
Long r = getAsLong(element);
return r == null ? 0l : r;
}
public static Integer getAsInteger(JsonElement element) {
return isNull(element) ? null : element.getAsInt();
}
public static int getAsPrimitiveInt(JsonElement element) {
Integer r = getAsInteger(element);
return r == null ? 0 : r;
}
public static Boolean getAsBoolean(JsonElement element) {
return isNull(element) ? null : element.getAsBoolean();
}
public static boolean getAsPrimitiveBool(JsonElement element) {
Boolean r = getAsBoolean(element);
return r == null ? false : r.booleanValue();
}
public static Double getAsDouble(JsonElement element) {
return isNull(element) ? null : element.getAsDouble();
}
public static double getAsPrimitiveDouble(JsonElement element) {
Double r = getAsDouble(element);
return r == null ? 0d : r;
}
public static Float getAsFloat(JsonElement element) {
return isNull(element) ? null : element.getAsFloat();
}
public static float getAsPrimitiveFloat(JsonElement element) {
Float r = getAsFloat(element);
return r == null ? 0f : r;
}
}

View File

@@ -0,0 +1,88 @@
/*
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
*
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
* arose from modification of the original source, or other redistribution of this source
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
*/
package chanjarster.weixin.util.json;
import java.lang.reflect.Type;
import chanjarster.weixin.api.WxConsts;
import chanjarster.weixin.bean.WxCustomMessage;
import chanjarster.weixin.bean.WxCustomMessage.WxArticle;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
/**
*
* @author qianjia
*
*/
public class WxCustomMessageGsonAdapter implements JsonSerializer<WxCustomMessage> {
public JsonElement serialize(WxCustomMessage message, Type typeOfSrc, JsonSerializationContext context) {
JsonObject messageJson = new JsonObject();
messageJson.addProperty("touser", message.getTouser());
messageJson.addProperty("msgtype", message.getMsgtype());
if (WxConsts.MSG_TEXT.equals(message.getMsgtype())) {
JsonObject text = new JsonObject();
text.addProperty("content", message.getContent());
messageJson.add("text", text);
}
if (WxConsts.MSG_IMAGE.equals(message.getMsgtype())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", message.getMedia_id());
messageJson.add("image", image);
}
if (WxConsts.MSG_VOICE.equals(message.getMsgtype())) {
JsonObject voice = new JsonObject();
voice.addProperty("media_id", message.getMedia_id());
messageJson.add("voice", voice);
}
if (WxConsts.MSG_VIDEO.equals(message.getMsgtype())) {
JsonObject video = new JsonObject();
video.addProperty("media_id", message.getMedia_id());
video.addProperty("thumb_media_id", message.getThumb_media_id());
video.addProperty("title", message.getTitle());
video.addProperty("description", message.getDescription());
messageJson.add("video", video);
}
if (WxConsts.MSG_MUSIC.equals(message.getMsgtype())) {
JsonObject music = new JsonObject();
music.addProperty("title", message.getTitle());
music.addProperty("description", message.getDescription());
music.addProperty("thumb_media_id", message.getThumb_media_id());
music.addProperty("musicurl", message.getMusicurl());
music.addProperty("hqmusicurl", message.getHqmusicurl());
messageJson.add("music", music);
}
if (WxConsts.MSG_NEWS.equals(message.getMsgtype())) {
JsonArray articleJsonArray = new JsonArray();
for (WxArticle article : message.getArticles()) {
JsonObject articleJson = new JsonObject();
articleJson.addProperty("title", article.getTitle());
articleJson.addProperty("description", article.getDescription());
articleJson.addProperty("url", article.getUrl());
articleJson.addProperty("picurl", article.getPicurl());
articleJsonArray.add(articleJson);
}
messageJson.add("articles", articleJsonArray);
}
return messageJson;
}
}

View File

@@ -0,0 +1,23 @@
package chanjarster.weixin.util.json;
import chanjarster.weixin.bean.WxCustomMessage;
import chanjarster.weixin.bean.WxMenu;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class WxGsonBuilder {
public static final GsonBuilder INSTANCE = new GsonBuilder();
static {
INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxCustomMessage.class, new WxCustomMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMenu.class, new WxMenuGsonAdapter());
}
public static Gson create() {
return INSTANCE.create();
}
}

View File

@@ -0,0 +1,95 @@
/*
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
*
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
* arose from modification of the original source, or other redistribution of this source
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
*/
package chanjarster.weixin.util.json;
import java.lang.reflect.Type;
import chanjarster.weixin.bean.WxMenu;
import chanjarster.weixin.bean.WxMenu.WxMenuButton;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
/**
*
* @author qianjia
*
*/
public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializer<WxMenu> {
public JsonElement serialize(WxMenu menu, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
JsonArray buttonArray = new JsonArray();
for (WxMenuButton button : menu.getButton()) {
JsonObject buttonJson = convertToJson(button);
buttonArray.add(buttonJson);
}
json.add("button", buttonArray);
return json;
}
protected JsonObject convertToJson(WxMenuButton button) {
JsonObject buttonJson = new JsonObject();
buttonJson.addProperty("type", button.getType());
buttonJson.addProperty("name", button.getName());
buttonJson.addProperty("key", button.getKey());
buttonJson.addProperty("url", button.getUrl());
if (button.getSub_button() != null && button.getSub_button().size() > 0) {
JsonArray buttonArray = new JsonArray();
for (WxMenuButton sub_button : button.getSub_button()) {
buttonArray.add(convertToJson(sub_button));
}
buttonJson.add("sub_button", buttonArray);
}
return buttonJson;
}
public WxMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
/*
* 操蛋的微信
* 创建菜单时是 { button : ... }
* 查询菜单时是 { menu : { button : ... } }
*/
WxMenu menu = new WxMenu();
JsonObject menuJson = json.getAsJsonObject().get("menu").getAsJsonObject();
JsonArray buttonsJson = menuJson.get("button").getAsJsonArray();
for (int i = 0; i < buttonsJson.size(); i++) {
JsonObject buttonJson = buttonsJson.get(i).getAsJsonObject();
WxMenuButton button = convertFromJson(buttonJson);
menu.getButton().add(button);
if (buttonJson.get("sub_button") == null || buttonJson.get("sub_button").isJsonNull()) {
continue;
}
JsonArray sub_buttonsJson = buttonJson.get("sub_button").getAsJsonArray();
for (int j = 0; j < sub_buttonsJson.size(); j++) {
JsonObject sub_buttonJson = sub_buttonsJson.get(j).getAsJsonObject();
button.getSub_button().add(convertFromJson(sub_buttonJson));
}
}
return menu;
}
protected WxMenuButton convertFromJson(JsonObject json) {
WxMenuButton button = new WxMenuButton();
button.setName(GsonHelper.getString(json, "name"));
button.setKey(GsonHelper.getString(json, "key"));
button.setUrl(GsonHelper.getString(json, "url"));
button.setType(GsonHelper.getString(json, "type"));
return button;
}
}