手把手教您开发JAVA微信SDK-发送消息

家好,今天我给大家带来的是微信开发之发送消息。

用户关注微信号,那么肯定是为了得到某种功能,假如用户发送文本信息“电话”,我们能给用户发送我的联系电话,这样是不是很友好呢?

好,昨天我们已经接受到了用户发送的信息,今天我们就对用户进行回复消息吧!

首先,回复消息有6个种类:

1 回复文本消息
2 回复图片消息
3 回复语音消息
4 回复视频消息
5 回复音乐消息
6 回复图文消息

我们先来做一个简单的回复文本消息的例子吧!

回复文本消息

  1. <xml>  
  2. <ToUserName><![CDATA[toUser]]></ToUserName>  
  3. <FromUserName><![CDATA[fromUser]]></FromUserName>  
  4. <CreateTime>12345678</CreateTime>  
  5. <MsgType><![CDATA[text]]></MsgType>  
  6. <Content><![CDATA[你好]]></Content>  
  7. </xml>  
1.新建发送消息基本对象com.ansitech.weixin.sdk.message.OutputMessage.java
  1. /* 
  2.  * 微信公众平台(JAVA) SDK 
  3.  * 
  4.  * Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved. 
  5.  * http://www.ansitech.com/weixin/sdk/ 
  6.  * 
  7.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  8.  * you may not use this file except in compliance with the License. 
  9.  * You may obtain a copy of the License at 
  10.  * 
  11.  *      http://www.apache.org/licenses/LICENSE-2.0 
  12.  * 
  13.  * Unless required by applicable law or agreed to in writing, software 
  14.  * distributed under the License is distributed on an "AS IS" BASIS, 
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  16.  * See the License for the specific language governing permissions and 
  17.  * limitations under the License. 
  18.  */  
  19. package com.ansitech.weixin.sdk.message;  
  20.   
  21. /** 
  22.  * 微信发送被动响应消息的抽象类 
  23.  * 
  24.  * <p>应用程序需要定义一个子类,来实现具体方法</p> 
  25.  * 
  26.  * @author qsyang<[email protected]> 
  27.  */  
  28. public abstract class OutputMessage implements java.io.Serializable {  
  29.   
  30.     /** 
  31.      * 接收方帐号(收到的OpenID) 
  32.      */  
  33.     private String ToUserName;  
  34.     /** 
  35.      * 开发者微信号 
  36.      */  
  37.     private String FromUserName;  
  38.     /** 
  39.      * 消息创建时间 (整型) 
  40.      */  
  41.     private Long CreateTime;  
  42.   
  43.     /** 
  44.      * 获取 接收方帐号(收到的OpenID) 
  45.      * 
  46.      * @return 接收方帐号(收到的OpenID) 
  47.      */  
  48.     public String getToUserName() {  
  49.         return ToUserName;  
  50.     }  
  51.   
  52.     /** 
  53.      * 设置 接收方帐号(收到的OpenID) 
  54.      * 
  55.      * @return 接收方帐号(收到的OpenID) 
  56.      */  
  57.     public String getFromUserName() {  
  58.         return FromUserName;  
  59.     }  
  60.   
  61.     /** 
  62.      * 获取 消息创建时间 (整型) 
  63.      * 
  64.      * @return 消息创建时间 (整型) 
  65.      */  
  66.     public Long getCreateTime() {  
  67.         return CreateTime;  
  68.     }  
  69.   
  70.     /** 
  71.      * 获取 消息类型 
  72.      * 
  73.      * @return 消息类型 
  74.      */  
  75.     public abstract String getMsgType();  
  76. }  
2.新建文本消息发送对象com.ansitech.weixin.sdk.message.TextOutputMessage.java
  1. /* 
  2.  * 微信公众平台(JAVA) SDK 
  3.  * 
  4.  * Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved. 
  5.  * http://www.ansitech.com/weixin/sdk/ 
  6.  * 
  7.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  8.  * you may not use this file except in compliance with the License. 
  9.  * You may obtain a copy of the License at 
  10.  * 
  11.  *      http://www.apache.org/licenses/LICENSE-2.0 
  12.  * 
  13.  * Unless required by applicable law or agreed to in writing, software 
  14.  * distributed under the License is distributed on an "AS IS" BASIS, 
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  16.  * See the License for the specific language governing permissions and 
  17.  * limitations under the License. 
  18.  */  
  19. package com.ansitech.weixin.sdk.message;  
  20.   
  21. /** 
  22.  * 这个类实现了<tt>OutputMessage</tt>,用来回复文本消息 
  23.  * 
  24.  * <p>提供了获取文本内容<code>getContent()</code>等主要方法.</p> 
  25.  * 
  26.  * @author qsyang<[email protected]> 
  27.  */  
  28. public class TextOutputMessage extends OutputMessage {  
  29.   
  30.     /** 
  31.      * 消息类型:文本消息 
  32.      */  
  33.     private String MsgType = "text";  
  34.     /** 
  35.      * 文本消息 
  36.      */  
  37.     private String Content;  
  38.   
  39.     /** 
  40.      * 创建一个新的 Output Message.并且MsgType的值为text. 
  41.      */  
  42.     public TextOutputMessage() {  
  43.     }  
  44.   
  45.     /** 
  46.      * 创建一个自定义文本内容content的Output Message. 
  47.      * 
  48.      * @param content 文本内容 
  49.      */  
  50.     public TextOutputMessage(String content) {  
  51.         Content = content;  
  52.     }  
  53.   
  54.     /** 
  55.      * 获取 消息类型 
  56.      * 
  57.      * @return 消息类型 
  58.      */  
  59.     @Override  
  60.     public String getMsgType() {  
  61.         return MsgType;  
  62.     }  
  63.   
  64.     /** 
  65.      * 获取 文本消息 
  66.      * 
  67.      * @return 文本消息 
  68.      */  
  69.     public String getContent() {  
  70.         return Content;  
  71.     }  
  72.   
  73.     /** 
  74.      * 设置 文本消息 
  75.      * 
  76.      * @param content 文本消息 
  77.      */  
  78.     public void setContent(String content) {  
  79.         Content = content;  
  80.     }  
  81. }  
3.修改com.ansitech.weixin.sdk.WeixinUrlFilter.java中接受文本消息部分代码。
  1. package com.ansitech.weixin.sdk;  
  2.   
  3. import com.ansitech.weixin.sdk.message.InputMessage;  
  4. import com.ansitech.weixin.sdk.message.MsgType;  
  5. import com.ansitech.weixin.sdk.message.OutputMessage;  
  6. import com.ansitech.weixin.sdk.message.TextOutputMessage;  
  7. import com.ansitech.weixin.sdk.util.SHA1;  
  8. import com.thoughtworks.xstream.XStream;  
  9. import com.thoughtworks.xstream.core.util.QuickWriter;  
  10. import com.thoughtworks.xstream.io.HierarchicalStreamWriter;  
  11. import com.thoughtworks.xstream.io.xml.DomDriver;  
  12. import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;  
  13. import com.thoughtworks.xstream.io.xml.XppDriver;  
  14. ...  
  15.   
  16. public class WeixinUrlFilter implements Filter {  
  17.   
  18.     ...  
  19.   
  20.     @Override  
  21.     public void doFilter(ServletRequest req, ServletResponse res,  
  22.             FilterChain chain) throws IOException, ServletException {  
  23.         HttpServletRequest request = (HttpServletRequest) req;  
  24.         HttpServletResponse response = (HttpServletResponse) res;  
  25.         ...  
  26.         if (isGet) {  
  27.             ...  
  28.         } else {  
  29.             ...  
  30.             //根据消息类型获取对应的消息内容  
  31.             if (msgType.equals(MsgType.Text.toString())) {  
  32.                 try {  
  33.                     //文本消息  
  34.                     System.out.println("开发者微信号:" + inputMsg.getToUserName());  
  35.                     System.out.println("发送方帐号:" + inputMsg.getFromUserName());  
  36.                     System.out.println("消息创建时间:" + inputMsg.getCreateTime());  
  37.                     System.out.println("消息内容:" + inputMsg.getContent());  
  38.                     System.out.println("消息Id:" + inputMsg.getMsgId());  
  39.                     //发送文本消息 start  
  40.                     XStream xstream = new XStream(new XppDriver() {  
  41.                         @Override  
  42.                         public HierarchicalStreamWriter createWriter(Writer out) {  
  43.                             return new PrettyPrintWriter(out) {  
  44.                                 @Override  
  45.                                 protected void writeText(QuickWriter writer,  
  46.                                                                 String text) {  
  47.                                     if (!text.startsWith("<![CDATA[")) {  
  48.                                         text = "<![CDATA[" + text + "]]>";  
  49.                                     }  
  50.                                     writer.write(text);  
  51.                                 }  
  52.                             };  
  53.                         }  
  54.                     });  
  55.                     //创建文本发送消息对象  
  56.                     TextOutputMessage outputMsg = new TextOutputMessage();  
  57.                     outputMsg.setContent("你的消息已经收到,谢谢!");  
  58.                     setOutputMsgInfo(outputMsg, inputMsg);  
  59.                     //设置对象转换的XML根节点为xml  
  60.                     xstream.alias("xml", outputMsg.getClass());  
  61.                     //将对象转换为XML字符串  
  62.                     String xml = xstream.toXML(outputMsg);  
  63.                     //将内容发送给微信服务器,发送到用户手机  
  64.                     response.getWriter().write(xml);  
  65.                 } catch (Exception ex) {  
  66.                     System.out.println("消息接受和发送出现异常!");  
  67.                     ex.printStackTrace();  
  68.                 }  
  69.             }  
  70.         }  
  71.     }  
  72.   
  73.     //设置详细信息  
  74.     private static void setOutputMsgInfo(OutputMessage oms,  
  75.                             InputMessage msg) throws Exception {  
  76.         // 设置发送信息  
  77.         Class<?> outMsg = oms.getClass().getSuperclass();  
  78.         Field CreateTime = outMsg.getDeclaredField("CreateTime");  
  79.         Field ToUserName = outMsg.getDeclaredField("ToUserName");  
  80.         Field FromUserName = outMsg.getDeclaredField("FromUserName");  
  81.   
  82.         ToUserName.setAccessible(true);  
  83.         CreateTime.setAccessible(true);  
  84.         FromUserName.setAccessible(true);  
  85.   
  86.         CreateTime.set(oms, new Date().getTime());  
  87.         ToUserName.set(oms, msg.getFromUserName());  
  88.         FromUserName.set(oms, msg.getToUserName());  
  89.     }  
  90.   
  91.     ...  
  92. }  
以上代码给出了发送消息的部分,“...”的部分请参考我的上俩篇文章。


如有疑问,请留言。

你可以微信关注我的订阅号:vzhanqun 微站管家

手把手教您开发JAVA微信SDK-发送消息