微信&java 开发2 发送文本消息并回复
2016-05-12 13:15
585 查看
因为用的是springmvc
首先看一下统一接收接口,dispatchWeiXinRequest发放用于区别get还是post
最后看一下MessageUtil
首先看一下统一接收接口,dispatchWeiXinRequest发放用于区别get还是post
@RequestMapping(value = "/dispatcher") @ResponseBody public void dispatcher(HttpServletRequest request,HttpServletResponse response) throws ParseException, org.dom4j.DocumentException { LOG.info("Execute action's WeiXin:execute() method. Start:{}",System.currentTimeMillis() + ""); try { /** * 分发请求 */ dispatchWeiXinRequest(request, response); } catch (ServletException e) { LOG.info("Execute action's WeiXin:execute() method Exception. {}",e.getMessage()); } catch (IOException e) { LOG.info("Execute action's WeiXin:execute() method Exception. {}",e.getMessage()); } LOG.info("Execute action's WeiXin:execute() method. End :{}",System.currentTimeMillis() + ""); }
private void dispatchWeiXinRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException, DocumentException { if ("GET".equals(request.getMethod().toUpperCase().toString())) { LOG.info("Execute action's WeiXin:dispatchWeiXinRequest() method. GET"); doGet(request, response); } else if ("POST".equals(request.getMethod().toUpperCase().toString())) { LOG.info("Execute action's WeiXin:dispatchWeiXinRequest() method. POST"); doPost(request, response); } }post方法
private void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException, DocumentException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); try { Map<String, String> map = MessageUtil.xmlToMap(request); String fromUserName = map.get("FromUserName"); String toUserName = map.get("ToUserName"); String msgType = map.get("MsgType"); String content = map.get("Content"); String message = null; if(MessageUtil.MESSAGE_TEXT.equals(msgType)){ if("1".equals(content)){ message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.firstMenu()); }else if("2".equals(content)){ message = MessageUtil.initNewsMessage(toUserName, fromUserName); } } out.print(message); } finally{ out.close(); } }
最后看一下MessageUtil
/** * 图文消息的组装 * @param toUserName * @param fromUserName * @return */ public static String initNewsMessage(String toUserName,String fromUserName){ String message = null; List<News> newsList = new ArrayList<News>(); NewsMessage newsMessage = new NewsMessage(); News news = new News(); news.setTitle("欢迎"); news.setDescription("Hello World。"); String localImg = "D:\\cc.jpg"; File srcFile = new File(localImg); String tarImg = ServletContextUtil.get().getRealPath("/images") + "\\cc2.jpg"; File tarFile = new File(tarImg); if(!tarFile.exists()) { try { FileCopyUtils.copy(srcFile,tarFile); } catch (IOException e) { e.printStackTrace(); } } news.setPicUrl(WeiXinPropUtil.WebDomain + "/WeiXin_SSM/images/cc2.jpg"); news.setUrl("www.imooc.com"); newsList.add(news); newsMessage.setToUserName(fromUserName); newsMessage.setFromUserName(toUserName); newsMessage.setCreateTime(new Date().getTime()); newsMessage.setMsgType(MESSAGE_NEWS); newsMessage.setArticles(newsList); newsMessage.setArticleCount(newsList.size()); message = newsMessageToXml(newsMessage); return message; }
public class News { private String Title; private String Description; private String PicUrl; private String Url; public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String getDescription() { return Description; } public void setDescription(String description) { Description = description; } public String getPicUrl() { return PicUrl; } public void setPicUrl(String picUrl) { PicUrl = picUrl; } public String getUrl() { return Url; } public void setUrl(String url) { Url = url; } }
public class NewsMessage extends BaseMessage{ private int ArticleCount; private List<News> Articles; public int getArticleCount() { return ArticleCount; } public void setArticleCount(int articleCount) { ArticleCount = articleCount; } public List<News> getArticles() { return Articles; } public void setArticles(List<News> articles) { Articles = articles; } }
/** * 图文消息转为xml * @param newsMessage * @return */ public static String newsMessageToXml(NewsMessage newsMessage){ XStream xstream = new XStream(); xstream.alias("xml", newsMessage.getClass()); xstream.alias("item", new News().getClass()); return xstream.toXML(newsMessage); }
相关文章推荐
- 微信&java 开发1 平台接入
- 仿微信开发(二)--系统语言设置
- 微信js-sdk config JavaScript签名
- Java小程序:模拟双色球开奖
- 如何在微信公众号下载保存图片??
- 微信支付
- 公众号判断用户是否已关注php代码
- Android 开发判断用户是否安装微信和QQ并跳转过去
- (高效开发)仿微信图片选择器PhotoPicker
- php微信开发中各种小技巧收集
- 微信对话框小三角形实现
- 我也有微信朋友圈了 Android实现
- 微信第三方授权登录返回错误信息: 40029
- WSTMall V1.6.0发布,支持QQ、微信登录,支持购物车信息插入数据库
- 产品知识分享:外链分享到微信的逻辑
- C#利用 Face++sdk 实现wpf小程序【1】 5/11/16
- 【原创】.Net 微信 JS-SDK图片、语音上传接口的实现(MVC)-(一 、上传图片)
- Ubuntu16.04 Genymotion 模拟器上安装微信
- C#开发微信门户及应用(39)--使用微信JSSDK实现签到的功能
- iOS 支付 [支付宝、银联、微信]