热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

微信公众帐号开发教程第4篇-消息及消息处理工具的封装

工欲善其事必先利其器!本篇内容主要讲解如何将微信公众平台定义的消息及消息相关的操作封装成工具类,方面后期的使用。这里需要明确的是消息其实是由用户发给你的公众帐号的,消息先被微信平台接收到,然后微信平台会将该消息转给你在开发模式接口配置中指定的URL地

工欲善其事必先利其器!本篇内容主要讲解如何将微信公众平台定义的消息及消息相关的操作封装成工具类,方面后期的使用。这里需要明确的是消息其实是由用户发给你的公众帐号的,消息先被微信平台接收到,然后微信平台会将该消息转给你在开发模式接口配置中指定的URL地址。


微信公众平台消息接口

要接收微信平台发送的消息,我们需要先熟悉微信公众平台API中消息接口部分,点此进入,点击后将进入到消息接口指南部分,如下图所示:

在上图左侧可以看到微信公众平台目前开放的接口有三种:消息接口、通用接口和自定义菜单接口。通用接口和自定义菜单接口只有拿到内测资格才能调用,而内测资格的申请也已经关闭了,我们只有期待将来某一天微信会对大众用户开放吧,所以没有内测资格的用户就不要再浪费时间在这两个接口上,只需要用好消息接口就可以了。


消息推送和消息回复

下面将主要介绍消息接口。对于消息的接收、响应我们只需要关注上图中的“4 消息推送”和“5 消息回复”就足够了。

我们先来了解接口中的“消息推送”指的是什么,点击“4 消息推送”,可以看到接口中的“消息推送”指的是“当普通用户向公众帐号发消息时,微信服务器将POST该消息到填写的URL上”,即这里定义的是用户能够发送哪些类型的消息、消息有哪些字段、消息被微信服务器以什么方式转发给我们的公众帐号后台。

消息推送中定义了我们将会接收到的消息类型有5种:文本消息、图片消息、地理位置消息、链接消息和事件推送,其实语音消息我们也能够接收到的,只不过拿不到具体的语音文件而以(需要内测资格才能够获取语音文件)。


接口中的“消息回复”定义了我们能回复给用户的消息类型、消息字段和消息格式,微信公众平台的接口指南中是这样描述的:

上面说到我们能回复给用户的消息有5种,但目前在开发模式下能回复的消息只有3种:文本消息、音乐消息和图文消息,而语音消息和视频消息目前只能在编辑模式下使用。


消息的封装

接下来要做的就是将消息推送(请求)、消息回复(响应)中定义的消息进行封装,建立与之对应的Java类(Java是一门面向对象的编程语言,封装后使用起来更方便),下面的请求消息是指消息推送中定义的消息,响应消息指消息回复中定义的消息。

请求消息的基类

把消息推送中定义的所有消息都有的字段提取出来,封装成一个基类,这些公有的字段包括:ToUserName(开发者微信号)、FromUserName(发送方帐号,OPEN_ID)、CreateTime(消息的创建时间)、MsgType(消息类型)、MsgId(消息ID),封装后基类org.liufeng.course.message.req.BaseMessage的代码如下:


[java] view plaincopyprint?
  1. package org.liufeng.course.message.req; 
  2.  
  3. /**
  4. * 消息基类(普通用户 -> 公众帐号)
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class BaseMessage { 
  10.     // 开发者微信号 
  11.     private String ToUserName; 
  12.     // 发送方帐号(一个OpenID) 
  13.     private String FromUserName; 
  14.     // 消息创建时间 (整型) 
  15.     private long CreateTime; 
  16.     // 消息类型(text/image/location/link) 
  17.     private String MsgType; 
  18.     // 消息id,64位整型 
  19.     private long MsgId; 
  20.  
  21.     public String getToUserName() { 
  22.         return ToUserName; 
  23.     } 
  24.  
  25.     public void setToUserName(String toUserName) { 
  26.         ToUserName = toUserName; 
  27.     } 
  28.  
  29.     public String getFromUserName() { 
  30.         return FromUserName; 
  31.     } 
  32.  
  33.     public void setFromUserName(String fromUserName) { 
  34.         FromUserName = fromUserName; 
  35.     } 
  36.  
  37.     public long getCreateTime() { 
  38.         return CreateTime; 
  39.     } 
  40.  
  41.     public void setCreateTime(long createTime) { 
  42.         CreateTime = createTime; 
  43.     } 
  44.  
  45.     public String getMsgType() { 
  46.         return MsgType; 
  47.     } 
  48.  
  49.     public void setMsgType(String msgType) { 
  50.         MsgType = msgType; 
  51.     } 
  52.  
  53.     public long getMsgId() { 
  54.         return MsgId; 
  55.     } 
  56.  
  57.     public void setMsgId(long msgId) { 
  58.         MsgId = msgId; 
  59.     } 
package org.liufeng.course.message.req;

/**
 * 消息基类(普通用户 -> 公众帐号)
 * 
 * @author liufeng
 * @date 2013-05-19
 */
public class BaseMessage {
	// 开发者微信号
	private String ToUserName;
	// 发送方帐号(一个OpenID)
	private String FromUserName;
	// 消息创建时间 (整型)
	private long CreateTime;
	// 消息类型(text/image/location/link)
	private String MsgType;
	// 消息id,64位整型
	private long MsgId;

	public String getToUserName() {
		return ToUserName;
	}

	public void setToUserName(String toUserName) {
		ToUserName = toUserName;
	}

	public String getFromUserName() {
		return FromUserName;
	}

	public void setFromUserName(String fromUserName) {
		FromUserName = fromUserName;
	}

	public long getCreateTime() {
		return CreateTime;
	}

	public void setCreateTime(long createTime) {
		CreateTime = createTime;
	}

	public String getMsgType() {
		return MsgType;
	}

	public void setMsgType(String msgType) {
		MsgType = msgType;
	}

	public long getMsgId() {
		return MsgId;
	}

	public void setMsgId(long msgId) {
		MsgId = msgId;
	}
}
请求消息之文本消息
[java] view plaincopyprint?
  1. package org.liufeng.course.message.req; 
  2.  
  3. /**
  4. * 文本消息
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class TextMessage extends BaseMessage { 
  10.     // 消息内容 
  11.     private String Content; 
  12.  
  13.     public String getContent() { 
  14.         return Content; 
  15.     } 
  16.  
  17.     public void setContent(String content) { 
  18.         COntent= content; 
  19.     } 
package org.liufeng.course.message.req;

/**
 * 文本消息
 * 
 * @author liufeng
 * @date 2013-05-19
 */
public class TextMessage extends BaseMessage {
	// 消息内容
	private String Content;

	public String getContent() {
		return Content;
	}

	public void setContent(String content) {
		COntent= content;
	}
}
请求消息之图片消息


[java] view plaincopyprint?
  1. package org.liufeng.course.message.req; 
  2.  
  3. /**
  4. * 图片消息
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class ImageMessage extends BaseMessage { 
  10.     // 图片链接 
  11.     private String PicUrl; 
  12.  
  13.     public String getPicUrl() { 
  14.         return PicUrl; 
  15.     } 
  16.  
  17.     public void setPicUrl(String picUrl) { 
  18.         PicUrl = picUrl; 
  19.     } 
package org.liufeng.course.message.req;

/**
 * 图片消息
 * 
 * @author liufeng
 * @date 2013-05-19
 */
public class ImageMessage extends BaseMessage {
	// 图片链接
	private String PicUrl;

	public String getPicUrl() {
		return PicUrl;
	}

	public void setPicUrl(String picUrl) {
		PicUrl = picUrl;
	}
}
请求消息之地理位置消息
[java] view plaincopyprint?
  1. package org.liufeng.course.message.req; 
  2.  
  3. /**
  4. * 地理位置消息
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class LocationMessage extends BaseMessage { 
  10.     // 地理位置维度 
  11.     private String Location_X; 
  12.     // 地理位置经度 
  13.     private String Location_Y; 
  14.     // 地图缩放大小 
  15.     private String Scale; 
  16.     // 地理位置信息 
  17.     private String Label; 
  18.  
  19.     public String getLocation_X() { 
  20.         return Location_X; 
  21.     } 
  22.  
  23.     public void setLocation_X(String location_X) { 
  24.         Location_X = location_X; 
  25.     } 
  26.  
  27.     public String getLocation_Y() { 
  28.         return Location_Y; 
  29.     } 
  30.  
  31.     public void setLocation_Y(String location_Y) { 
  32.         Location_Y = location_Y; 
  33.     } 
  34.  
  35.     public String getScale() { 
  36.         return Scale; 
  37.     } 
  38.  
  39.     public void setScale(String scale) { 
  40.         Scale = scale; 
  41.     } 
  42.  
  43.     public String getLabel() { 
  44.         return Label; 
  45.     } 
  46.  
  47.     public void setLabel(String label) { 
  48.         Label = label; 
  49.     } 
package org.liufeng.course.message.req;

/**
 * 地理位置消息
 * 
 * @author liufeng
 * @date 2013-05-19
 */
public class LocationMessage extends BaseMessage {
	// 地理位置维度
	private String Location_X;
	// 地理位置经度
	private String Location_Y;
	// 地图缩放大小
	private String Scale;
	// 地理位置信息
	private String Label;

	public String getLocation_X() {
		return Location_X;
	}

	public void setLocation_X(String location_X) {
		Location_X = location_X;
	}

	public String getLocation_Y() {
		return Location_Y;
	}

	public void setLocation_Y(String location_Y) {
		Location_Y = location_Y;
	}

	public String getScale() {
		return Scale;
	}

	public void setScale(String scale) {
		Scale = scale;
	}

	public String getLabel() {
		return Label;
	}

	public void setLabel(String label) {
		Label = label;
	}
}
请求消息之链接消息
[java] view plaincopyprint?
  1. package org.liufeng.course.message.req; 
  2.  
  3. /**
  4. * 链接消息
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class LinkMessage extends BaseMessage { 
  10.     // 消息标题 
  11.     private String Title; 
  12.     // 消息描述 
  13.     private String Description; 
  14.     // 消息链接 
  15.     private String Url; 
  16.  
  17.     public String getTitle() { 
  18.         return Title; 
  19.     } 
  20.  
  21.     public void setTitle(String title) { 
  22.         Title = title; 
  23.     } 
  24.  
  25.     public String getDescription() { 
  26.         return Description; 
  27.     } 
  28.  
  29.     public void setDescription(String description) { 
  30.         Description = description; 
  31.     } 
  32.  
  33.     public String getUrl() { 
  34.         return Url; 
  35.     } 
  36.  
  37.     public void setUrl(String url) { 
  38.         Url = url; 
  39.     } 
package org.liufeng.course.message.req;

/**
 * 链接消息
 * 
 * @author liufeng
 * @date 2013-05-19
 */
public class LinkMessage extends BaseMessage {
	// 消息标题
	private String Title;
	// 消息描述
	private String Description;
	// 消息链接
	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 getUrl() {
		return Url;
	}

	public void setUrl(String url) {
		Url = url;
	}
}
请求消息之语音消息
[java] view plaincopyprint?
  1. package org.liufeng.course.message.req; 
  2.  
  3. /**
  4. * 音频消息
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class VoiceMessage extends BaseMessage { 
  10.     // 媒体ID 
  11.     private String MediaId; 
  12.     // 语音格式 
  13.     private String Format; 
  14.  
  15.     public String getMediaId() { 
  16.         return MediaId; 
  17.     } 
  18.  
  19.     public void setMediaId(String mediaId) { 
  20.         MediaId = mediaId; 
  21.     } 
  22.  
  23.     public String getFormat() { 
  24.         return Format; 
  25.     } 
  26.  
  27.     public void setFormat(String format) { 
  28.         Format = format; 
  29.     } 
package org.liufeng.course.message.req;

/**
 * 音频消息
 * 
 * @author liufeng
 * @date 2013-05-19
 */
public class VoiceMessage extends BaseMessage {
	// 媒体ID
	private String MediaId;
	// 语音格式
	private String Format;

	public String getMediaId() {
		return MediaId;
	}

	public void setMediaId(String mediaId) {
		MediaId = mediaId;
	}

	public String getFormat() {
		return Format;
	}

	public void setFormat(String format) {
		Format = format;
	}
}
响应消息的基类

同样,把消息回复中定义的所有消息都有的字段提取出来,封装成一个基类,这些公有的字段包括:ToUserName(接收方帐号,用户的OPEN_ID)、FromUserName(开发者的微信号)、CreateTime(消息的创建时间)、MsgType(消息类型)、FuncFlag(消息的星标标识),封装后基类org.liufeng.course.message.resp.BaseMessage的代码如下:


[java] view plaincopyprint?
  1. package org.liufeng.course.message.resp; 
  2.  
  3. /**
  4. * 消息基类(公众帐号 -> 普通用户)
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class BaseMessage { 
  10.     // 接收方帐号(收到的OpenID) 
  11.     private String ToUserName; 
  12.     // 开发者微信号 
  13.     private String FromUserName; 
  14.     // 消息创建时间 (整型) 
  15.     private long CreateTime; 
  16.     // 消息类型(text/music/news) 
  17.     private String MsgType; 
  18.     // 位0x0001被标志时,星标刚收到的消息 
  19.     private int FuncFlag; 
  20.  
  21.     public String getToUserName() { 
  22.         return ToUserName; 
  23.     } 
  24.  
  25.     public void setToUserName(String toUserName) { 
  26.         ToUserName = toUserName; 
  27.     } 
  28.  
  29.     public String getFromUserName() { 
  30.         return FromUserName; 
  31.     } 
  32.  
  33.     public void setFromUserName(String fromUserName) { 
  34.         FromUserName = fromUserName; 
  35.     } 
  36.  
  37.     public long getCreateTime() { 
  38.         return CreateTime; 
  39.     } 
  40.  
  41.     public void setCreateTime(long createTime) { 
  42.         CreateTime = createTime; 
  43.     } 
  44.  
  45.     public String getMsgType() { 
  46.         return MsgType; 
  47.     } 
  48.  
  49.     public void setMsgType(String msgType) { 
  50.         MsgType = msgType; 
  51.     } 
  52.  
  53.     public int getFuncFlag() { 
  54.         return FuncFlag; 
  55.     } 
  56.  
  57.     public void setFuncFlag(int funcFlag) { 
  58.         FuncFlag = funcFlag; 
  59.     } 
package org.liufeng.course.message.resp;

/**
 * 消息基类(公众帐号 -> 普通用户)
 * 
 * @author liufeng
 * @date 2013-05-19
 */
public class BaseMessage {
	// 接收方帐号(收到的OpenID)
	private String ToUserName;
	// 开发者微信号
	private String FromUserName;
	// 消息创建时间 (整型)
	private long CreateTime;
	// 消息类型(text/music/news)
	private String MsgType;
	// 位0x0001被标志时,星标刚收到的消息
	private int FuncFlag;

	public String getToUserName() {
		return ToUserName;
	}

	public void setToUserName(String toUserName) {
		ToUserName = toUserName;
	}

	public String getFromUserName() {
		return FromUserName;
	}

	public void setFromUserName(String fromUserName) {
		FromUserName = fromUserName;
	}

	public long getCreateTime() {
		return CreateTime;
	}

	public void setCreateTime(long createTime) {
		CreateTime = createTime;
	}

	public String getMsgType() {
		return MsgType;
	}

	public void setMsgType(String msgType) {
		MsgType = msgType;
	}

	public int getFuncFlag() {
		return FuncFlag;
	}

	public void setFuncFlag(int funcFlag) {
		FuncFlag = funcFlag;
	}
}
响应消息之文本消息


[java] view plaincopyprint?
  1. package org.liufeng.course.message.resp; 
  2.  
  3. /**
  4. * 文本消息
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class TextMessage extends BaseMessage { 
  10.     // 回复的消息内容 
  11.     private String Content; 
  12.  
  13.     public String getContent() { 
  14.         return Content; 
  15.     } 
  16.  
  17.     public void setContent(String content) { 
  18.         COntent= content; 
  19.     } 
package org.liufeng.course.message.resp;

/**
 * 文本消息
 * 
 * @author liufeng
 * @date 2013-05-19
 */
public class TextMessage extends BaseMessage {
	// 回复的消息内容
	private String Content;

	public String getContent() {
		return Content;
	}

	public void setContent(String content) {
		COntent= content;
	}
}
响应消息之音乐消息


[java] view plaincopyprint?
  1. package org.liufeng.course.message.resp; 
  2.  
  3. /**
  4. * 音乐消息
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class MusicMessage extends BaseMessage { 
  10.     // 音乐 
  11.     private Music Music; 
  12.  
  13.     public Music getMusic() { 
  14.         return Music; 
  15.     } 
  16.  
  17.     public void setMusic(Music music) { 
  18.         Music = music; 
  19.     } 
package org.liufeng.course.message.resp;

/**
 * 音乐消息
 * 
 * @author liufeng
 * @date 2013-05-19
 */
public class MusicMessage extends BaseMessage {
	// 音乐
	private Music Music;

	public Music getMusic() {
		return Music;
	}

	public void setMusic(Music music) {
		Music = music;
	}
}
音乐消息中Music类的定义
[java] view plaincopyprint?
  1. package org.liufeng.course.message.resp; 
  2.  
  3. /**
  4. * 音乐model
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class Music { 
  10.     // 音乐名称 
  11.     private String Title; 
  12.     // 音乐描述 
  13.     private String Description; 
  14.     // 音乐链接 
  15.     private String MusicUrl; 
  16.     // 高质量音乐链接,WIFI环境优先使用该链接播放音乐 
  17.     private String HQMusicUrl; 
  18.  
  19.     public String getTitle() { 
  20.         return Title; 
  21.     } 
  22.  
  23.     public void setTitle(String title) { 
  24.         Title = title; 
  25.     } 
  26.  
  27.     public String getDescription() { 
  28.         return Description; 
  29.     } 
  30.  
  31.     public void setDescription(String description) { 
  32.         Description = description; 
  33.     } 
  34.  
  35.     public String getMusicUrl() { 
  36.         return MusicUrl; 
  37.     } 
  38.  
  39.     public void setMusicUrl(String musicUrl) { 
  40.         MusicUrl = musicUrl; 
  41.     } 
  42.  
  43.     public String getHQMusicUrl() { 
  44.         return HQMusicUrl; 
  45.     } 
  46.  
  47.     public void setHQMusicUrl(String musicUrl) { 
  48.         HQMusicUrl = musicUrl; 
  49.     } 
  50.  
package org.liufeng.course.message.resp;

/**
 * 音乐model
 * 
 * @author liufeng
 * @date 2013-05-19
 */
public class Music {
	// 音乐名称
	private String Title;
	// 音乐描述
	private String Description;
	// 音乐链接
	private String MusicUrl;
	// 高质量音乐链接,WIFI环境优先使用该链接播放音乐
	private String HQMusicUrl;

	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 getMusicUrl() {
		return MusicUrl;
	}

	public void setMusicUrl(String musicUrl) {
		MusicUrl = musicUrl;
	}

	public String getHQMusicUrl() {
		return HQMusicUrl;
	}

	public void setHQMusicUrl(String musicUrl) {
		HQMusicUrl = musicUrl;
	}

}
响应消息之图文消息


[java] view plaincopyprint?
  1. package org.liufeng.course.message.resp; 
  2.  
  3. import java.util.List; 
  4.  
  5. /**
  6. * 文本消息
  7. *
  8. * @author liufeng
  9. * @date 2013-05-19
  10. */ 
  11. public class NewsMessage extends BaseMessage { 
  12.     // 图文消息个数,限制为10条以内 
  13.     private int ArticleCount; 
  14.     // 多条图文消息信息,默认第一个item为大图 
  15.     private List
    Articles; 
  16.  
  17.     public int getArticleCount() { 
  18.         return ArticleCount; 
  19.     } 
  20.  
  21.     public void setArticleCount(int articleCount) { 
  22.         ArticleCount = articleCount; 
  23.     } 
  24.  
  25.     public List
    getArticles() { 
  26.         return Articles; 
  27.     } 
  28.  
  29.     public void setArticles(List
    articles) { 
  30.         Articles = articles; 
  31.     } 
package org.liufeng.course.message.resp;

import java.util.List;

/**
 * 文本消息
 * 
 * @author liufeng
 * @date 2013-05-19
 */
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类的定义
[java] view plaincopyprint?
  1. package org.liufeng.course.message.resp; 
  2.  
  3. /**
  4. * 图文model
  5. *
  6. * @author liufeng
  7. * @date 2013-05-19
  8. */ 
  9. public class Article { 
  10.     // 图文消息名称 
  11.     private String Title; 
  12.     // 图文消息描述 
  13.     private String Description; 
  14.     // 图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80,限制图片链接的域名需要与开发者填写的基本资料中的Url一致 
  15.     private String PicUrl; 
  16.     // 点击图文消息跳转链接 
  17.     private String Url; 
  18.  
  19.     public String getTitle() { 
  20.         return Title; 
  21.     } 
  22.  
  23.     public void setTitle(String title) { 
  24.         Title = title; 
  25.     } 
  26.  
  27.     public String getDescription() { 
  28.         return null == Description ? "" : Description; 
  29.     } 
  30.  
  31.     public void setDescription(String description) { 
  32.         Description = description; 
  33.     } 
  34.  
  35.     public String getPicUrl() { 
  36.         return null == PicUrl ? "" : PicUrl; 
  37.     } 
  38.  
  39.     public void setPicUrl(String picUrl) { 
  40.         PicUrl = picUrl; 
  41.     } 
  42.  
  43.     public String getUrl() { 
  44.         return null == Url ? "" : Url; 
  45.     } 
  46.  
  47.     public void setUrl(String url) { 
  48.         Url = url; 
  49.     } 
  50.  
package org.liufeng.course.message.resp;

/**
 * 图文model
 * 
 * @author liufeng
 * @date 2013-05-19
 */
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;
	}

}

全部消息封装完成后,Eclipse工程中关于消息部分的结构应该与下图保持一致,如果不一致的(类名、属性名称不一致的)请检查后调整一致,因为后面的章节还要介绍如何将微信开发中通用的类方法、与业务无关的工具类封装打成jar包,以后再做微信项目只需要引入该jar包即可,这种工作做一次就可以了。


如何解析请求消息?

接下来解决请求消息的解析问题。微信服务器会将用户的请求通过doPost方法发送给我们,让我们再来回顾下上一章节已经写好的doPost方法的定义:


[java] view plaincopyprint?
  1. /**
  2.     * 处理微信服务器发来的消息
  3.     */   
  4.    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
  5.        // TODO 消息的接收、处理、响应   
  6.    }   
 /** 
     * 处理微信服务器发来的消息 
     */  
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        // TODO 消息的接收、处理、响应  
    }  
doPost方法有两个参数,request中封装了请求相关的所有内容,可以从request中取出微信服务器发来的消息;而通过response我们可以对接收到的消息进行响应,即发送消息。


那么如何解析请求消息的问题也就转化为如何从request中得到微信服务器发送给我们的xml格式的消息了。这里我们借助于开源框架dom4j去解析xml(这里使用的是dom4j-1.6.1.jar),然后将解析得到的结果存入HashMap,解析请求消息的方法如下:


[java] view plaincopyprint?
  1. /**
  2. * 解析微信发来的请求(XML)
  3. *
  4. * @param request
  5. * @return
  6. * @throws Exception
  7. */ 
  8. @SuppressWarnings("unchecked"
  9. public static Map parseXml(HttpServletRequest request) throws Exception { 
  10.     // 将解析结果存储在HashMap中 
  11.     Map map = new HashMap(); 
  12.  
  13.     // 从request中取得输入流 
  14.     InputStream inputStream = request.getInputStream(); 
  15.     // 读取输入流 
  16.     SAXReader reader = new SAXReader(); 
  17.     Document document = reader.read(inputStream); 
  18.     // 得到xml根元素 
  19.     Element root = document.getRootElement(); 
  20.     // 得到根元素的所有子节点 
  21.     List elementList = root.elements(); 
  22.  
  23.     // 遍历所有子节点 
  24.     for (Element e : elementList) 
  25.         map.put(e.getName(), e.getText()); 
  26.  
  27.     // 释放资源 
  28.     inputStream.close(); 
  29.     inputStream = null
  30.  
  31.     return map; 
/**
 * 解析微信发来的请求(XML)
 * 
 * @param request
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static Map parseXml(HttpServletRequest request) throws Exception {
	// 将解析结果存储在HashMap中
	Map map = new HashMap();

	// 从request中取得输入流
	InputStream inputStream = request.getInputStream();
	// 读取输入流
	SAXReader reader = new SAXReader();
	Document document = reader.read(inputStream);
	// 得到xml根元素
	Element root = document.getRootElement();
	// 得到根元素的所有子节点
	List elementList = root.elements();

	// 遍历所有子节点
	for (Element e : elementList)
		map.put(e.getName(), e.getText());

	// 释放资源
	inputStream.close();
	inputStream = null;

	return map;
}
如何将响应消息转换成xml返回?


我们先前已经将响应消息封装成了Java类,方便我们在代码中使用。那么,请求接收成功、处理完成后,该如何将消息返回呢?这里就涉及到如何将响应消息转换成xml返回的问题,这里我们将采用开源框架xstream来实现Java类到xml的转换(这里使用的是xstream-1.3.1.jar),代码如下:


[java] view plaincopyprint?
  1. /**
  2. * 文本消息对象转换成xml
  3. *
  4. * @param textMessage 文本消息对象
  5. * @return xml
  6. */ 
  7. public static String textMessageToXml(TextMessage textMessage) { 
  8.     xstream.alias("xml", textMessage.getClass()); 
  9.     return xstream.toXML(textMessage); 
  10.  
  11. /**
  12. * 音乐消息对象转换成xml
  13. *
  14. * @param musicMessage 音乐消息对象
  15. * @return xml
  16. */ 
  17. public static String musicMessageToXml(MusicMessage musicMessage) { 
  18.     xstream.alias("xml", musicMessage.getClass()); 
  19.     return xstream.toXML(musicMessage); 
  20.  
  21. /**
  22. * 图文消息对象转换成xml
  23. *
  24. * @param newsMessage 图文消息对象
  25. * @return xml
  26. */
推荐阅读
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社区 版权所有