遇到的坑:
40027 不合法的子菜单按钮URL长度
在这里,我明明把url与微信开发文档上换成一样的了,但是还是会爆这个错。。后来我发现是我转换的JSON格式中 URL是大写的字样,而开发文档上是小写的。
所以需要改自己定义的ViewButton类中用到的get set方法中属性名字。
同理,如果说遇到这类问题,确保自己URL或者KEY是合格的,那么肯定就是JSON的问题啦。
关于花生壳的使用
因为自从上次使用到现在,我发现花生壳新产生的域名后面会带上端口号!!!
而微信要正确连接到自己映射出的本地服务器的话,需要80端口。所以我觉得花生壳在这点上似乎不能用了QAQ。所以我又去用了ngrok。
https://ngrok.com 这是他们的官网。下载过后,再登录。按照首页教你的方法就可以操作啦~~~~
正式开始这次的总结
微信消息接收与发送
XML
将xml变成Map
public static Map<String,String> xmlToMap(HttpServletRequest request){
Map<String, String> map = new HashMap<String, String>();
SAXReader reader = new SAXReader();
InputStream inputStream;
try {
inputStream = request.getInputStream();
Document doc = reader.read(inputStream);
Element root = doc.getRootElement();
List<Element> list = root.elements();
getEliments(list, map);
inputStream.close();
return map;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
private static void getEliments(List<Element> sonEliment, Map<String, String> map) {
for (Element element : sonEliment) {
if (element.elements().size() != 0) {
getEliments(element.elements(), map);
} else {
map.put(element.getName(),element.getText());
}
}
}
将对象变为XML
提前封装一个TextMessage的类
//其它6中消息参见开发文档
private String ToUserName;
private String FromUserName;
private String CreateTime;
private String MsgType;
private String Content;
private String MsgId;
//。。。
public String TextMessageToXML(TextMessage textMessage){
XStream xStream = new XStream();
xStream.alias("xml", textMessage.getClass());//根标签一定是xml
return xStream.toXML(textMessage);
}
JSON
String menu = JSONObject.fromObject(InitMenu.initMenu()).toString();
//这里就只说将拼装的菜单转为json格式吧
菜单
菜单的种类不说了(详情参见文档)
请求方式
POST
public static JSONObject doPost(String url,String outStr){
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = null;
try {
httpPost.setEntity(new StringEntity(outStr,"utf-8"));
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if(entity != null){
String result = EntityUtils.toString(entity,"UTF-8");
jsonObject =JSONObject.fromObject(result);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
GET
public static JSONObject doGet(String url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
JSONObject jsonObject = null;
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
//entity 转为 String , String再转为JSONObject
String result = EntityUtils.toString(entity, "UTF-8");
jsonObject = JSONObject.fromObject(result);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}