最近公司需要拿微信公众平台做个东西,所以就开始了最基本学习,网上很多是php版的,对于我这个只会java,不会php的就只能在网上找点只言片语来一点一点学习了。不费话了直接贴图看效果(很简单的)。
不会做关注时就回复信息,所以必须先发送“?”,才能出现提示。回复“1”和“其他”都是返回文字信息,“2”是音乐信息,“3”是图文信息。
好了,下面进入正题。
1、首先必须开通微信公中平台,还得申请到高级功能的(这个点比较坑,我等了两天才申请到的),关于申请公众平台我就不说了,网上很多介绍,自己看吧。
2、必须有自己的服务器资源,因为公众平台开发者模式开启时,必须填写接口配置信息(成为开发者)。
服务器资源我是用的是百度开发者中心来创建自己的应用。百度开发者中心:http://develope
baidu.com/
成为微信公众平台开发者前必须发布好自己的服务器资源,因为微信服务器将发送GET请求到填写的URL上(后面详细介绍)。
3、创建自己的应用。
这是整个项目结构。
下面逐一介绍:
1 public class WbChat extends HttpServlet{
2
3 private static final long serialVersionUID = 1L;
4
5 public WbChat() {
6 super();
7 }
8 @Override
9 protected void doGet(HttpServletRequest request, HttpServletResponse response)
10 throws ServletException, IOException {
11 //这里没有对发送信息者进行验证,直接返回了,要加验证的话自己去百度吧
12 response.setContentType("text/html;charset=UTF-8");
13 PrintWriter pw = response.getWriter();
14 String echo = request.getParameter("echostr");
15 echo = new String(echo.getBytes("ISO-8859-1"),"UTF-8");
16 pw.println(echo);
17 //当你提交成为开发者的URL和Token时,微信服务器将发送GET请求到填写的URL上,
18 //只有你返回参数echostr时,他才会认为你这个接口是通的,才会提交成功
19 }
20
21 @Override
22 protected void doPost(HttpServletRequest request, HttpServletResponse response)
23 throws ServletException, IOException {
24 // 当你用微信给平台发送信息时就会到这里
25 // 回复音乐和图文消息,我都写死了,自己可以根据自己的需要加相应的处理
26 response.setContentType("text/html;charset=UTF-8");
27 PrintWriter pw = response.getWriter();
28 String wxMsgXml = IOUtils.toString(request.getInputStream(),"utf-8");
29 RequestTextMessage textMsg = null;
30 try {
31 textMsg = getRequestTextMessage(wxMsgXml);
32 } catch (Exception e) {
33 e.printStackTrace();
34 }
35 StringBuffer replyMsg = new StringBuffer();
36 String receive = textMsg.getContent().trim();
37 String returnXml = null;
38
39 if (textMsg != null&&!receive.equals("")) {
40 if (receive.equals("?")||receive.equals("?")) {
41
42 replyMsg.append("欢迎使用微信平台!");
43 replyMsg.append("\r\n1、当前时间");
44 replyMsg.append("\r\n2、听音乐");
45 replyMsg.append("\r\n3、看图文");
46 replyMsg.append("\r\n其他、回音壁请直接输入文字信息");
47
48 returnXml = getReplyTextMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
49
50 } else if (receive.equals("2")) {
51
52 //回复音乐信息
53 returnXml = getReplyMusicMessage(textMsg.getFromUserName(),textMsg.getToUserName());
54
55
56 } else if (receive.equals("3")) {
57
58 //回复图文
59 returnXml = getReplyTuwenMessage(textMsg.getFromUserName(),textMsg.getToUserName());
60
61 } else if (receive.equals("1")) {
62
63 //回复时间
64 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
65
66 replyMsg.append("当前时间\r\n"+df.format(new Date()));
67 returnXml = getReplyTextMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
68
69 } else {
70
71 replyMsg.append("收到: " + textMsg.getContent());
72 returnXml = getReplyTextMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
73
74 }
75 } else {
76
77 replyMsg.append("别闹了,这谁看得懂啊!");
78 returnXml = getReplyTextMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
79
80 }
81 pw.println(returnXml);
82 }
83
84
85 // 获取推送文本消息
86 private RequestTextMessage getRequestTextMessage(String xml){
87
88 XStream xstream = new XStream(new DomDriver());
89
90 xstream.alias("xml", RequestTextMessage.class);
91 xstream.aliasField("ToUserName", RequestTextMessage.class, "toUserName");
92 xstream.aliasField("FromUserName", RequestTextMessage.class, "fromUserName");
93 xstream.aliasField("CreateTime", RequestTextMessage.class, "createTime");
94 xstream.aliasField("MsgType", RequestTextMessage.class, "messageType");
95 xstream.aliasField("Content", RequestTextMessage.class, "content");
96 xstream.aliasField("MsgId", RequestTextMessage.class, "msgId");
97
98 RequestTextMessage requestTextMessage = (RequestTextMessage)xstream.fromXML(xml);
99 return requestTextMessage;
100 }
101 // 回复文本消息
102 private String getReplyTextMessage(String content, String fromUserName,String toUserName){
103
104 ReplyTextMessage we = new ReplyTextMessage();
105 we.setMessageType("text");
106 we.setFuncFlag("0");
107 we.setCreateTime(new Long(new Date().getTime()).toString());
108 we.setContent(content);
109 we.setToUserName(fromUserName);
110 we.setFromUserName(toUserName);
111 XStream xstream = new XStream(new DomDriver());
112 xstream.alias("xml", ReplyTextMessage.class);
113 xstream.aliasField("ToUserName", ReplyTextMessage.class, "toUserName");
114 xstream.aliasField("FromUserName", ReplyTextMessage.class, "fromUserName");
115 xstream.aliasField("CreateTime", ReplyTextMessage.class, "createTime");
116 xstream.aliasField("MsgType", ReplyTextMessage.class, "messageType");
117 xstream.aliasField("Content", ReplyTextMessage.class, "content");
118 xstream.aliasField("FuncFlag", ReplyTextMessage.class, "funcFlag");
119 String xml =xstream.toXML(we);
120 return xml;
121 }
122 // 回复音乐消息
123 private String getReplyMusicMessage(String fromUserName,String toUserName){
124
125 ReplyMusicMessage we = new ReplyMusicMessage();
126 Music music = new Music();
127
128 we.setMessageType("music");
129 we.setCreateTime(new Long(new Date().getTime()).toString());
130 we.setToUserName(fromUserName);
131 we.setFromUserName(toUserName);
132 we.setFuncFlag("0");
133
134 music.setTitle("回家|X-man");
135 music.setDescription("萨克斯 回家 勾起你想家的愁思...");
136
137 String url = "http://bcs.duapp.com/yishi-music/%E5%9B%9E%E5%AE%B6.mp3?sign=MBO:97068c69ccb2ab230a497c59d528dcce:LdYZ%2FLXohKa6YCy9gbxL%2B1mZ4Co%3D";
138 String url2 = "http://bcs.duapp.com/yishi-music/X-man.mp3?sign=MBO:97068c69ccb2ab230a497c59d528dcce:cYV%2B%2Fq2Tlv2de6gqecZynCyIm3k%3D";
139 music.setMusicUrl(url);
140 music.setHqMusicUrl(url2);
141
142 we.setMusic(music);
143
144 XStream xstream = new XStream(new DomDriver());
145 xstream.alias("xml", ReplyMusicMessage.class);
146 xstream.aliasField("ToUserName", ReplyMusicMessage.class, "toUserName");
147 xstream.aliasField("FromUserName", ReplyMusicMessage.class, "fromUserName");
148 xstream.aliasField("CreateTime", ReplyMusicMessage.class, "createTime");
149 xstream.aliasField("MsgType", ReplyMusicMessage.class, "messageType");
150 xstream.aliasField("FuncFlag", ReplyMusicMessage.class, "funcFlag");
151 xstream.aliasField("Music", ReplyMusicMessage.class, "Music");
152
153 xstream.aliasField("Title", Music.class, "title");
154 xstream.aliasField("Description", Music.class, "description");
155 xstream.aliasField("MusicUrl", Music.class, "musicUrl");
156 xstream.aliasField("HQMusicUrl", Music.class, "hqMusicUrl");
157
158 String xml =xstream.toXML(we);
159 return xml;
160 }
161
162 // 回复图文消息
163 private String getReplyTuwenMessage(String fromUserName,String toUserName){
164
165 ReplyTuwenMessage we = new ReplyTuwenMessage();
166
167 Articles articles = new Articles();
168
169 Item item = new Item();
170
171 we.setMessageType("news");
172 we.setCreateTime(new Long(new Date().getTime()).toString());
173 we.setToUserName(fromUserName);
174 we.setFromUserName(toUserName);
175 we.setFuncFlag("0");
176 we.setArticleCount(1);
177
178 item.setTitle("俊介");
179 item.setDescription("俊介(SHUNSUKE)是Twitter上现在最流行的偶像犬,是哈多利系博美犬(即俗称英系博美),因为在网上卖萌而走红网络。");
180 item.setPicUrl("http://bcs.duapp.com/yishi-music/111.jpg?sign=MBO:97068c69ccb2ab230a497c59d528dcce:hmzcBYxgI4yUaTd9GvahO1GvE%2BA%3D");
181 item.setUrl("http://baike.baidu.com/view/6300265.htm");
182
183 articles.setItem(item);
184 we.setArticles(articles);
185
186 XStream xstream = new XStream(new DomDriver());
187 xstream.alias("xml", ReplyTuwenMessage.class);
188 xstream.aliasField("ToUserName", ReplyTuwenMessage.class, "toUserName");
189 xstream.aliasField("FromUserName", ReplyTuwenMessage.class, "fromUserName");
190 xstream.aliasField("CreateTime", ReplyTuwenMessage.class, "createTime");
191 xstream.aliasField("MsgType", ReplyTuwenMessage.class, "messageType");
192 xstream.aliasField("Articles", ReplyTuwenMessage.class, "Articles");
193
194 xstream.aliasField("ArticleCount", ReplyTuwenMessage.class, "articleCount");
195 xstream.aliasField("FuncFlag", ReplyTuwenMessage.class, "funcFlag");
196
197 xstream.aliasField("item", Articles.class, "item");
198
199 xstream.aliasField("Title", Item.class, "title");
200 xstream.aliasField("Description", Item.class, "description");
201 xstream.aliasField("PicUrl", Item.class, "picUrl");
202 xstream.aliasField("Url", Item.class, "url");
203
204 String xml =xstream.toXML(we);
205 return xml;
206 }
207
208 }
WbChat
1 public class ReplyMusicMessage {
2
3 private String toUserName;
4 private String fromUserName;
5 private String createTime;
6 private String messageType;
7 private Music Music;
8 private String funcFlag;
9 //这里的funcFlag,微信平台接口文档里没有,但是必须写上,不然会收不到返回的信息
10 //getters、setters
11 }
ReplyMusicMessage
1 public class ReplyTextMessage {
2
3 private String toUserName;
4 private String fromUserName;
5 private String createTime;
6 private String messageType;
7 private String content;
8 private String funcFlag;
9 // getters,setters
10 }
ReplyTextMessage
1 public class ReplyTuwenMessage {
2
3 private String toUserName;
4 private String fromUserName;
5 private String createTime;
6 private String messageType;
7 private int articleCount;
8
9 private Articles Articles;
10 private String funcFlag;
11 //getters,setters
12 }
ReplyTuwenMessage
1 public class RequestTextMessage {
2 private String toUserName;
3 private String fromUserName;
4 private String createTime;
5 private String messageType;
6 private String content;
7 private String msgId;
8 //getters,setters
9 }
RequestTextMessage
1 public class Articles {
2
3 private Item item;
4
5 public Item getItem() {
6 return item;
7 }
8
9 public void setItem(Item item) {
10 this.item = item;
11 }
12
13
14 }
Articles
1 public class Item {
2
3 private String title;
4 private String description;
5 private String picUrl;
6 private String url;
7 //getters,setters
8 }
Item
1 public class Music {
2
3 private String title;
4 private String description;
5 private String musicUrl;
6 private String hqMusicUrl;
7 //getters,setters
8 }
Music
1 <servlet>
2 <servlet-name>Test</servlet-name>
3 <servlet-class>com.main.WbChat</servlet-class>
4 </servlet>
5 <servlet-mapping>
6 <servlet-name>Test</servlet-name>
7 <url-pattern>/wbchat</url-pattern>
8 </servlet-mapping>
web.xml配置还要加四个jar包:
利用MyEclipse直接把工程打为war包,上传到百度开发者中心,并上线该应用。
4、成为开发者,开启开发模式。
将百度开发者中心中的当前域名+/wbchat 添加到微信平台的URL处,Token可以随便填(因为我没有判断Token)。
5、大功告成,可以在微信上添加该公众账号聊天了。
小提示:
1、调试时,微信发送消息却没有受到信息,可以去看看百度开发者中心,在应用版本管理中找到该应用,然后点击高级下的日志,查看日志来看出什么问题了
2、下载jar包地址:http://jarfiles.pandaidea.com/
3、不要听百度开发者中心说的那个”如何提交代码“,它是使用svn,它给的下载svn地址下的是svn1.8的,根本就checkout不了工程,如果想使用svn的话,就得去下载svn1.6的版本来使用。我是使用上传更新war包来更新工程的,这样在自己的MyEclipse中直接编写。