上回说到 接口连接成功,接下来是真正的开发了。
消息的接收,整个过程就是关注订阅号的用户在微信订阅号中发送消息,微信服务器接收到消息,将消息发给开发者的服务器,服务器接收消息然后可以根据内容进行回复。
上代码,同样是在weixin.java的servlet中doPOst方法中接收消息
目录
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String notityXml = "";
String inputLine;
try {
while ((inputLine = request.getReader().readLine()) != null) {//将消息一行行读入
notityXml += inputLine;//将一行行的 xml数据存入字符串中
}
request.getReader().close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("接收到的报文:" + notityXml);//下面是对xml报文你的解析
Map m = parseXmlToList2(notityXml);//该函数将xml报文的项和值存入map数组中
System.out.println("接收的用户消息:"+m.get("Content").toString());
String receive=m.get("Content").toString();//得到用户输入的内容
private static Map parseXmlToList2(String xml) {
Map retMap = new HashMap();
try {
StringReader read = new StringReader(xml);
// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(read);
// 创建一个新的SAXBuilder
SAXBuilder sb = new SAXBuilder();
// 通过输入源构造一个Document
Document doc = (Document) sb.build(source);
Element root = doc.getRootElement();// 指向根节点
List<Element> es = root.getChildren();
if (es != null && es.size() != 0) {
for (Element element : es) {
retMap.put(element.getName(), element.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return retMap;
}
接下来是根据用户发送的内容进行回复,回复的是带参数的xml格式的报文
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
StringBuffer replyMsg = new StringBuffer();
String returnXml = null;
if(receive.equals("1")){
replyMsg=replyMsg.append("我长得帅吗?");
returnXml = getReplyTextMessage(replyMsg.toString(), m.get("FromUserName").toString(), m.get("ToUserName").toString());
}
else if(receive.equals("2")){
replyMsg=replyMsg.append("我长得帅吗?");
returnXml = getReplyPicMessage(m.get("FromUserName").toString(), m.get("ToUserName").toString());
}
else{
replyMsg=replyMsg.append("发送1:回答问题,发送2:收到图片");
returnXml = getReplyTextMessage(replyMsg.toString(), m.get("FromUserName").toString(), m.get("ToUserName").toString());
}
pw.println(returnXml);
private String getReplyTextMessage(String content, String fromUserName,String toUserName)
{
ReplyTextMessage we = new ReplyTextMessage();
we.setMessageType("text");
we.setFuncFlag("0");
we.setCreateTime(new Long(new Date().getTime()).toString());
we.setContent(content);
we.setToUserName(fromUserName);
we.setFromUserName(toUserName);
XStream xstream = new XStream(new DomDriver());
xstream.alias("xml", ReplyTextMessage.class);
xstream.aliasField("ToUserName", ReplyTextMessage.class, "toUserName");
xstream.aliasField("FromUserName", ReplyTextMessage.class, "fromUserName");
xstream.aliasField("CreateTime", ReplyTextMessage.class, "createTime");
xstream.aliasField("MsgType", ReplyTextMessage.class, "messageType");
xstream.aliasField("Content", ReplyTextMessage.class, "content");
xstream.aliasField("FuncFlag", ReplyTextMessage.class, "funcFlag");
String xml =xstream.toXML(we);
return xml;
}
// 得到转化格式的恢复的图片消息
private String getReplyPicMessage(String fromUserName,String toUserName){
ReplyPicMessage we = new ReplyPicMessage();
Articles articles = new Articles();
Item item = new Item();
we.setMessageType("news");
we.setCreateTime(new Long(new Date().getTime()).toString());
we.setToUserName(fromUserName);
we.setFromUserName(toUserName);
we.setFuncFlag("0");
we.setArticleCount(1);
item.setTitle("俊介");
item.setDescription("俊介(SHUNSUKE)是Twitter上现在最流行的偶像犬,是哈多利系博美犬(即俗称英系博美),因为在网上卖萌而走红网络。");
item.setPicUrl("http://jiangzhongxi.tunnel.2bdata.com/weixin/junjie.jpg");
item.setUrl("http://baike.baidu.com/view/6300265.htm");
articles.setItem(item);
we.setArticles(articles);
XStream xstream = new XStream(new DomDriver());
xstream.alias("xml", ReplyPicMessage.class);
xstream.aliasField("ToUserName", ReplyPicMessage.class, "toUserName");
xstream.aliasField("FromUserName", ReplyPicMessage.class, "fromUserName");
xstream.aliasField("CreateTime", ReplyPicMessage.class, "createTime");
xstream.aliasField("MsgType", ReplyPicMessage.class, "messageType");
xstream.aliasField("Articles", ReplyPicMessage.class, "Articles");
xstream.aliasField("ArticleCount", ReplyPicMessage.class, "articleCount");
xstream.aliasField("FuncFlag", ReplyPicMessage.class, "funcFlag");
xstream.aliasField("item", Articles.class, "item");
xstream.aliasField("Title", Item.class, "title");
xstream.aliasField("Description", Item.class, "description");
xstream.aliasField("PicUrl", Item.class, "picUrl");
xstream.aliasField("Url", Item.class, "url");
String xml =xstream.toXML(we);
return xml;
}
private Item item;
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
public class Item {
private String title;
private String description;
private String picUrl;
private String url;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
public class ReplyPicMessage {
private String toUserName;
private String fromUserName;
private String createTime;
private String messageType;
private int articleCount;
private Articles Articles;
public String getToUserName() {
return toUserName;
}
public void setToUserName(String toUserName) {
this.toUserName = toUserName;
}
public String getFromUserName() {
return fromUserName;
}
public void setFromUserName(String fromUserName) {
this.fromUserName = fromUserName;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public int getArticleCount() {
return articleCount;
}
public void setArticleCount(int articleCount) {
this.articleCount = articleCount;
}
public Articles getArticles() {
return Articles;
}
public void setArticles(Articles articles) {
Articles = articles;
}
public String getFuncFlag() {
return funcFlag;
}
public void setFuncFlag(String funcFlag) {
this.funcFlag = funcFlag;
}
private String funcFlag;
}
public class ReplyTextMessage {
private String toUserName;
private String fromUserName;
private String createTime;
private String messageType;
private String content;
public String getToUserName() {
return toUserName;
}
public void setToUserName(String toUserName) {
this.toUserName = toUserName;
}
public String getFromUserName() {
return fromUserName;
}
public void setFromUserName(String fromUserName) {
this.fromUserName = fromUserName;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getFuncFlag() {
return funcFlag;
}
public void setFuncFlag(String funcFlag) {
this.funcFlag = funcFlag;
}
private String funcFlag;
}