最近在微信公众号开发中,根据需求需要实现扫一扫,弹出提示框进行操作,不过在获取二维码扫描结果ScanResult时,显示为null,
经过查询资料得到了解决方法:
是XML格式返回map集合时,没有获取子节点的值:
这是修改后的xmlToMap方法:
public static Map<String,String> xmlToMap(HttpServletRequest request){
Map<String, String> map = new HashMap<String,String>();
SAXReader reader = new SAXReader();
try {
//从request中获取输入流
InputStream inputStream = request.getInputStream();
Document document = reader.read(inputStream);
//获取xml的根节点
Element root = document.getRootElement();
//获取根元素的所有子节点
List<Element> list = root.elements();
//遍历list
for(Element element:list){
//遍历的结果放到集合中
map.put(element.getName(), element.getText());
List<Element> elementsSon = element.elements();
for(Element elementSon:elementsSon){
//遍历的结果放到集合中
map.put(elementSon.getName(), elementSon.getText());
}
}
//关闭流
inputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
//返回map集合
return map;
}
这是修改前的xmlToMap方法:
public static Map<String,String> xmlToMap(HttpServletRequest request){
Map<String, String> map = new HashMap<String,String>();
SAXReader reader = new SAXReader();
try {
//从request中获取输入流
InputStream inputStream = request.getInputStream();
Document document = reader.read(inputStream);
//获取xml的根节点
Element root = document.getRootElement();
//获取根元素的所有子节点
List<Element> list = root.elements();
//遍历list
for(Element element:list){
//遍历的结果放到集合中
map.put(element.getName(), element.getText());
}
//关闭流
inputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
//返回map集合
return map;
}
两者之间的区别在于:根据微信公众号平台开发者文档显示推送子节点没有获取完整,之前xmlToMap方法无法获得ScanResult节点,导致获取为null。
下面是获得ScanResul的完整方法:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
//接收消息
Map<String, String> map = MessageUtil.xmlToMap(req);
//开发者微信号
String toUserName = map.get("ToUserName");
//发送方账号
String fromUserName = map.get("FromUserName");
//消息类型
String msgType = map.get("MsgType");
//消息内容
String content = map.get("Content");
String msg = null;
//PrintWriter把xml消息返回给微信后台
PrintWriter printWriter = resp.getWriter();
if (MessageUtil.MESSAGE_EVENT.equals(msgType)){
//获得点击类型
String eventType = map.get("Event");
if (MessageUtil.MESSAGE_SCANCODE_WAITMSG.equals(eventType)){
String result = map.get("ScanResult");
msg = MessageUtil.initNewsFruitMessage(toUserName, fromUserName,result);
}
}
printWriter.print(msg);
printWriter.close();
}
下面是 initNewsMessage方法:
public static String initNewsFruitMessage(String toUserName,String fromUserName,String url){
String message = null;
List<News> newsList = new ArrayList<>();
NewsMessage newsMessage = new NewsMessage();
News news = new News();
news.setTitle("标题");
news.setDescription("描述");
news.setPicUrl("图片url");
news.setUrl(url);
newsList.add(news);
//设置图文消息的属性
newsMessage.setToUserName(fromUserName);
newsMessage.setFromUserName(toUserName);
newsMessage.setCreateTime(new Date().getTime());
newsMessage.setMsgType("news");
//设置消息体
newsMessage.setArticles(newsList);
newsMessage.setArticleCount(newsList.size());
message = newsMessageToXml(newsMessage);
return message;
}
下面是newsMessageToXml方法:
/**
* 图文消息转换--将对象类型转化为xml类型
*/
public static String newsMessageToXml(NewsMessage message){
XStream xStream = new XStream();
xStream.alias("xml", message.getClass());
xStream.alias("item", new News().getClass());
return xStream.toXML(message);
}
ok,完成。