热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java对接微信第三篇之订阅发送图文消息给用户

在第二篇的基础上,把订阅响应事件下的,发送文本消息接口替换成发送图文消息的接口。创建图文消息NewsMessagenewsMessagenewNewsM

在第二篇的基础上,把订阅响应事件下的,发送文本消息接口替换成发送图文消息的接口。

// 创建图文消息NewsMessage newsMessage = new NewsMessage();newsMessage.setToUserName(fromUserName);newsMessage.setFromUserName(toUserName);newsMessage.setCreateTime(System.currentTimeMillis());newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);newsMessage.setFuncFlag(0);List

articleList = new ArrayList
();Article article = new Article();article.setTitle("达达超人");article.setDescription("谢谢您的关注");article.setPicUrl("https://avatar.csdn.net/1/E/1/3_qq_31122833.jpg");article.setUrl("https://blog.csdn.net/qq_31122833");articleList.add(article);newsMessage.setArticleCount(articleList.size());newsMessage.setArticles(articleList);message = MessageUtil.newsMessageToXml(newsMessage);

其中:NewsMessage

public class NewsMessage extends BaseMessage { // 图文消息个数,限制为10条以内private int ArticleCount; // 多条图文消息信息,默认第一个item为大图private List

Articles; public int getArticleCount() { return ArticleCount; } public void setArticleCount(int articleCount) { ArticleCount = articleCount; } public List
getArticles() { return Articles; } public void setArticles(List
articles) { Articles = articles; }
}

Article:

public class Article { // 图文消息名称 private String Title;// 图文消息描述 private String Description;// 图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80,限制图片链接的域名需要与开发者填写的基本资料中的Url一致 private String PicUrl;// 点击图文消息跳转链接 private String Url; public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String getDescription() { return null == Description ? "" : Description; } public void setDescription(String description) { Description = description; } public String getPicUrl() { return null == PicUrl ? "" : PicUrl; } public void setPicUrl(String picUrl) { PicUrl = picUrl; } public String getUrl() { return null == Url ? "" : Url; } public void setUrl(String url) { Url = url; } }

MessageUtil:

public class MessageUtil {private static Logger logger &#61; Logger.getLogger(MessageUtil.class);/*** 返回消息类型&#xff1a;文本*/public static final String RESP_MESSAGE_TYPE_TEXT &#61; "text";/*** 返回消息类型&#xff1a;音乐*/public static final String RESP_MESSAGE_TYPE_MUSIC &#61; "music";/*** 返回消息类型&#xff1a;图文*/public static final String RESP_MESSAGE_TYPE_NEWS &#61; "news";/*** 请求消息类型&#xff1a;文本*/public static final String REQ_MESSAGE_TYPE_TEXT &#61; "text";/*** 请求消息类型&#xff1a;图片*/public static final String REQ_MESSAGE_TYPE_IMAGE &#61; "image";/*** 请求消息类型&#xff1a;链接*/public static final String REQ_MESSAGE_TYPE_LINK &#61; "link";/*** 请求消息类型&#xff1a;地理位置*/public static final String REQ_MESSAGE_TYPE_LOCATION &#61; "location";/*** 请求消息类型&#xff1a;音频*/public static final String REQ_MESSAGE_TYPE_VOICE &#61; "voice";/*** 请求消息类型&#xff1a;推送*/public static final String REQ_MESSAGE_TYPE_EVENT &#61; "event";/*** 事件类型&#xff1a;群发消息返回*/public static final String EVENT_TYPE_MASSSENDJOBFINISH &#61; "MASSSENDJOBFINISH";/*** 事件类型&#xff1a;小程序审核通过*/public static final String EVENT_TYPE_SMALLLROUTINE_PASS &#61; "weapp_audit_success";/*** 事件类型&#xff1a;小程序审核通过*/public static final String EVENT_TYPE_SMALLLROUTINE_FAIL &#61; "weapp_audit_fail";/*** 事件类型&#xff1a;subscribe(订阅)*/public static final String EVENT_TYPE_SUBSCRIBE &#61; "subscribe";/*** 事件类型&#xff1a;LOCATION(地理位置推送)*/public static final String EVENT_TYPE_LOCATION &#61; "LOCATION";/*** 事件类型&#xff1a;unsubscribe(取消订阅)*/public static final String EVENT_TYPE_UNSUBSCRIBE &#61; "unsubscribe";/*** 领取卡券事件*/public static final String EVENT_TYPE_USER_GET_CARD&#61;"user_get_card";public static final String EVENT_TYPE_COUPONS_GET &#61; "GetCoupons";/*** 卡券审核通过*/public static final String EVENT_TYPE_CARD_PASS_CHECK&#61;"card_pass_check";/*** 用户删除卡券*/public static final String EVENT_USER_DEL_CARD&#61;"user_del_card";/*** 卡券审核不通过*/public static final String EVENT_TYPE_CARD_NOT_PASS_CHECK&#61;"card_not_pass_check";/*** 微信自定义菜单文字点击事件*/public static final String EVENT_TYPE_WECHATMENU_TEXT_CHECK&#61;"wechat_menu_text";/*** 微信自定义菜单图文点击事件*/public static final String EVENT_TYPE_WECHATMENU_NEWS_CHECK&#61;"wechat_menu_news";/*** */public static final String EVENT_TYPE_WIFI_CONNECT &#61;"WifiConnected";/*** 微信门店审核通过*/public static final String EVENT_TYPE_POI_CHECK_NOTIFY&#61;"poi_check_notify";/*** 事件类型&#xff1a;CLICK(自定义菜单点击事件)*/public static final String EVENT_TYPE_CLICK &#61; "CLICK";/** 组装文本消息*/public static String textMsg(String toUserName,String fromUserName,String content){TextMessage text &#61; new TextMessage();text.setFromUserName(toUserName);text.setToUserName(fromUserName);text.setMsgType(REQ_MESSAGE_TYPE_TEXT);text.setCreateTime(new Date().getTime());text.setContent(content);return textMessageToXml(text);}/** 响应订阅事件--回复文本消息*/public static String subscribeForText(String toUserName,String fromUserName){return textMsg(toUserName, fromUserName, "欢迎关注&#xff0c;精彩内容不容错过&#xff01;&#xff01;&#xff01;");}/** 响应取消订阅事件*/public static String unsubscribe(String toUserName,String fromUserName){//TODO 可以进行取关后的其他后续业务处理System.out.println("用户&#xff1a;"&#43; fromUserName &#43;"取消关注~");return "";}/*** 解析微信发来的请求&#xff08;XML&#xff09;* * &#64;param request* &#64;return* &#64;throws Exception*/&#64;SuppressWarnings("unchecked")public static Map parseXml(HttpServletRequest request) throws Exception {// 将解析结果存储在HashMap中Map map &#61; new HashMap();// 从request中取得输入流InputStream inputStream &#61; request.getInputStream();// 读取输入流SAXReader reader &#61; new SAXReader();Document document &#61; reader.read(inputStream);// 得到xml根元素Element root &#61; document.getRootElement();// 得到根元素的所有子节点List elementList &#61; root.elements();// 遍历所有子节点for (Element e : elementList)map.put(e.getName(), e.getText());// 释放资源inputStream.close();inputStream &#61; null;return map;}/*** 文本消息对象转换成xml* * &#64;param textMessage 文本消息对象* &#64;return xml*/public static String textMessageToXml(TextMessage textMessage) {xstream.alias("xml", textMessage.getClass());return xstream.toXML(textMessage);}/*** 文本消息对象转换成xml* * &#64;param voiceMessage 语音消息对象* &#64;return xml*/public static String messageToXml(VoiceMessage voiceMessage) {xstream.alias("xml", voiceMessage.getClass());return xstream.toXML(voiceMessage);}/*** 文本消息对象转换成xml* * &#64;param videoMessage 视频消息对象* &#64;return xml*/public static String messageToXml(VideoMessage videoMessage) {xstream.alias("xml", videoMessage.getClass());return xstream.toXML(videoMessage);}/*** 音乐消息对象转换成xml* * &#64;param musicMessage 音乐消息对象* &#64;return xml*/public static String musicMessageToXml(MusicMessage musicMessage) {xstream.alias("xml", musicMessage.getClass());return xstream.toXML(musicMessage);}/*** 图文消息对象转换成xml* * &#64;param newsMessage 图文消息对象* &#64;return xml*/public static String newsMessageToXml(NewsMessage newsMessage) {xstream.alias("xml", newsMessage.getClass());xstream.alias("item", new Article().getClass());return xstream.toXML(newsMessage);}/*** 扩展xstream&#xff0c;使其支持CDATA块* * &#64;date 2013-05-19*/private static XStream xstream &#61; new XStream(new XppDriver() {public HierarchicalStreamWriter createWriter(Writer out) {return new PrettyPrintWriter(out) {// 对所有xml节点的转换都增加CDATA标记boolean cdata &#61; true;&#64;SuppressWarnings("unchecked")public void startNode(String name, Class clazz) {super.startNode(name, clazz);}protected void writeText(QuickWriter writer, String text) {if (cdata) {writer.write("");} else {writer.write(text);}}};}});/** * 判断是否是QQ表情 * * &#64;param content * &#64;return */ public static boolean isQqFace(String content) { boolean result &#61; false; // 判断QQ表情的正则表达式 String qqfaceRegex &#61; "/::\\)|/::~|/::B|/::\\||/:8-\\)|/::<|/::$|/::X|/::Z|/::&#39;\\(|/::-\\||/::&#64;|/::P|/::D|/::O|/::\\(|/::\\&#43;|/:--b|/::Q|/::T|/:,&#64;P|/:,&#64;-D|/::d|/:,&#64;o|/::g|/:\\|-\\)|/::!|/::L|/::>|/::,&#64;|/:,&#64;f|/::-S|/:\\?|/:,&#64;x|/:,&#64;&#64;|/::8|/:,&#64;!|/:!!!|/:xx|/:bye|/:wipe|/:dig|/:handclap|/:&-\\(|/:B-\\)|/:<&#64;|/:&#64;>|/::-O|/:>-\\||/:P-\\(|/::&#39;\\||/:X-\\)|/::\\*|/:&#64;x|/:8\\*|/:pd|/:|/:beer|/:basketb|/:oo|/:coffee|/:eat|/:pig|/:rose|/:fade|/:showlove|/:heart|/:break|/:cake|/:li|/:bome|/:kn|/:footb|/:ladybug|/:shit|/:moon|/:sun|/:gift|/:hug|/:strong|/:weak|/:share|/:v|/:&#64;\\)|/:jj|/:&#64;&#64;|/:bad|/:lvu|/:no|/:ok|/:love|/:|/:jump|/:shake|/:|/:circle|/:kotow|/:turn|/:skip|/:oY|/:#-0|/:hiphot|/:kiss|/:<&|/:&>"; Pattern p &#61; Pattern.compile(qqfaceRegex); Matcher m &#61; p.matcher(content); if (m.matches()) { result &#61; true; } return result; } /** * 演示Java中常用的获取long类型时间的两种方式 */ public static void main(String[] args) { long longTime &#61; 1373206143378L; String stdFormatTime &#61; formatTime(longTime); // 输出格式&#xff1a;2013-07-07 22:09:03 logger.info(stdFormatTime); } /** * 将long类型的时间转换成标准格式&#xff08;yyyy-MM-dd HH:mm:ss&#xff09; * * &#64;param longTime * &#64;return */ public static String formatTime(long longTime) { DateFormat format &#61; new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(new Date(longTime)); }
}

效果&#xff1a;

 


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了在Java中gt、gtgt、gtgtgt和lt之间的区别。通过解释符号的含义和使用例子,帮助读者理解这些符号在二进制表示和移位操作中的作用。同时,文章还提到了负数的补码表示和移位操作的限制。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
author-avatar
忧伤的嫖客
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有