首页>代码>Java通过Spring MVC开发的微信公众平台开发框架>/wechat-develop-framework-master/src/wechat-framework/src/main/java/com/xgzhang/wechat/framework/MenuUpdater.java
package com.xgzhang.wechat.framework; import java.io.StringWriter; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig; import org.json.JSONObject; import com.xgzhang.wechat.framework.domain.menu.ButtonWrap; public class MenuUpdater { private String _appId; private String _secret; private MenuFactory _menuFactory; public MenuUpdater(String appId, String secret, MenuFactory menuFactory){ this._appId = appId; this._secret = secret; this._menuFactory = menuFactory; } public int update() throws Exception { // Access token HttpGet tokenHttpGet = new HttpGet(String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", this._appId, this._secret)); HttpClient httpClient = new DefaultHttpClient(); HttpResponse tokenResponse = httpClient.execute(tokenHttpGet); byte[] tokenBytes = EntityUtils.toByteArray(tokenResponse.getEntity()); String token = (new JSONObject(new String(tokenBytes, "utf8"))).getString("access_token"); // Delete old menus HttpGet deleteHttpGet = new HttpGet("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + token); HttpResponse deleteResponse = httpClient.execute(deleteHttpGet); byte[] deleteBytes = EntityUtils.toByteArray(deleteResponse.getEntity()); int errorCode = (new JSONObject(new String(deleteBytes, "utf8"))).getInt("errcode"); if(errorCode != 0) return errorCode; // Create new menus HttpPost createPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + token); StringEntity contentEntity = new StringEntity(createMenuJsonString()); createPost.setEntity(contentEntity); HttpResponse createResponse = httpClient.execute(createPost); byte[] createBytes = EntityUtils.toByteArray(createResponse.getEntity()); errorCode = (new JSONObject(new String(createBytes, "utf8"))).getInt("errcode"); return errorCode; } private String createMenuJsonString() throws Exception { String jsonStr = null; ObjectMapper mapper = new ObjectMapper(); mapper.disable(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS); mapper.disable(SerializationConfig.Feature.WRITE_NULL_PROPERTIES); StringWriter sw = new StringWriter(); JsonGenerator gen = new JsonFactory().createJsonGenerator(sw); mapper.writeValue(gen, new ButtonWrap(this._menuFactory.generateMenu())); gen.close(); jsonStr = sw.toString(); return jsonStr; } }
最近下载更多
最近浏览更多
348835269 LV11
2023年9月27日
wuying8208 LV15
2023年7月27日
微信网友_6561552570798080 LV1
2023年7月14日
肖录婷 LV1
2023年6月10日
leixiao1 LV1
2023年5月14日
井晓码
2023年4月14日
暂无贡献等级
suxin123_123 LV5
2023年2月3日
263648 LV7
2022年12月26日
gch666 LV6
2022年12月22日
lywang LV7
2022年12月6日