把字符串转化成对象.
{"access_token":"zTZFxLrhM1vHAjXmq1ymk1JT8nlOKja4urFIr9PWqLYvFH9ZbWo8R6BmQqUr7VbArzBoZWOfBNI_fYGlO37r5t9s5jeYlxXXg4UXISCz2G9fOWvwzrOM86e8rv9f1eSOGYVhAHAPHE","expires_in":7200}
这段是网络上给你返回回来的东西.gson.fromJson()之后是转化成一个对象
return "AccessToken [access_token=" + access_token + ", expires_in="
+ expires_in + "]";
AccessToken [access_token=PQ_cs-BXBgspo0JNSlW_ytV3virET-nsK5muSsfVQ02qcp1dHDDK86dDWZ3WwDxMgT0_zqhCGaHq-Y1F6rjLXJnxN9d3EAThzkmtaMduYmXxAL98pt2a03HNAFXBESjjVVWdABARRS, expires_in=7200]
获得token之后首先是构建出来菜单的报文(JSON串).首先是要分析JSON串的组成方式.
定义菜单的Bean对象.Bean对象不是微信核心的东西.
菜单这一块是很复杂的,AccessToken.java是定义了的一个Bean,为什么要有token?因为菜单的参数需要token。菜单接口的参数需要token。
CaiDan也是一个Bean.WX_Info是保存微信的信息,获取token需要用的.WX_Token是得到token的信息。
这一块结合的东西多了,它要和token结合在一起使用。
创建菜单这一块和之前获得token都是网络操作,但是它们还是有区别的。
创建菜单需要传递token参数.
Sets the value of the doInput field for this URLConnection to the specified value.
A URL connection can be used for input and/or output. Set the DoInput flag to true if you intend to use the URL connection for input, false if not. The default is true.
package wx.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import bean.caidan.CaiDan;
public class WX_CaiDan {
/*
* create菜单
*/
//public static void createCaiDan() {
//public static void create(String access_token,List<CaiDan> caiDanList) {//token,
public static String create(String access_token,List<CaiDan> caiDanList) {//token,
HttpURLConnection connection = null;
StringBuffer strb = new StringBuffer();
try {
URL url = new URL("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token);
connection= (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStream out = connection.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
BufferedWriter bwriter = new BufferedWriter(writer);
//?????????????????????????????
bwriter.write("");
bwriter.flush();
InputStream in = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader breader = new BufferedReader(reader);
String str = null;
while(null!=( str = breader.readLine())){
strb.append(str);
}
out.close();
writer.close();
bwriter.close();
in.close();
reader.close();
breader.close();
} //catch (MalformedURLException e) {
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//
if(null!=connection){
connection.disconnect();
}
//return access_token;
return strb.toString();
}
}
package bean.caidan;
import java.util.List;
public class CaiDan {
//private String sub_button = null;// 否 二级菜单数组,个数应为1~5个
private List<CaiDan> sub_button = null;// 否 二级菜单数组,个数应为1~5个
private String type = null;// 是 菜单的响应动作类型,view表示网页类型,click表示点击类型,miniprogram表示小程序类型
private String name = null;// 是 菜单标题,不超过16个字节,子菜单不超过60个字节
private String key = null;// click等点击类型必须 菜单KEY值,用于消息接口推送,不超过128字节
private String url = null;// view、miniprogram类型必须 网页链接,用户点击菜单可打开链接,不超过1024字节。type为miniprogram时,不支持小程序的老版本客户端将打开本url。
//media_id media_id类型和view_limited类型必须 调用新增永久素材接口返回的合法media_id
//appid
//miniprogram类型必须 小程序的appid(仅认证公众号可配置)
//pagepath
//miniprogram类型必须 小程序的页面路径
public List<CaiDan> getSub_button() {
return sub_button;
}
public CaiDan() {
super();
// TODO Auto-generated constructor stub
}
/*
public CaiDan(List<CaiDan> sub_button, String type, String name,
String key, String url) {*/
public CaiDan(String type, String name,
String key, String url) {
//super();
//this.sub_button = sub_button;
this.type = type;
this.name = name;
this.key = key;
this.url = url;
}
public void setSub_button(List<CaiDan> sub_button) {
this.sub_button = sub_button;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
12312312