前面說到的要開發微信公眾平臺的功能首先要成為開發者,就是填寫url和tocken,我寫了兩篇但是系統說已經存在了所以我就不多說了,現在開發進入到開發的階段
首先新建一個java web 工程,名字自己取點finish就可以了。
接下來就是程式碼:下面我會借用柳老師的程式碼給大家看
把訊息推送中定義的所有訊息都有的欄位提取出來,封裝成一個基類,這些公有
的欄位包括:ToUserName(開發者微訊號)、FromUserName(傳送方帳號,OPEN_ID)、CreateTime(訊息的建立時
間)、MsgType(訊息型別)、MsgId(訊息ID),封裝後基類
org.liufeng.course.message.req.BaseMessage的程式碼如下:
package org.liufeng.course.message.req;
/**
* 訊息基類(普通使用者 -> 公眾帳號)
*
* @author liufeng
* @date 2013-05-19
*/
publicclass BaseMessage {
// 開發者微訊號
private String ToUserName;
// 傳送方帳號(一個OpenID)
private String FromUserName;
// 訊息建立時間 (整型)
privatelong CreateTime;
// 訊息型別(text/image/location/link)
private String MsgType;
// 訊息id,64位整型
privatelong MsgId;
public String getToUserName() {
return ToUserName;
}
publicvoid setToUserName(String toUserName) {
ToUserName = toUserName;
}
public String getFromUserName() {
return FromUserName;
}
publicvoid setFromUserName(String fromUserName) {
FromUserName = fromUserName;
}
publiclong getCreateTime() {
return CreateTime;
}
publicvoid setCreateTime(long createTime) {
CreateTime = createTime;
}
public String getMsgType() {
return MsgType;
}
publicvoid setMsgType(String msgType) {
MsgType = msgType;
}
publiclong getMsgId() {
return MsgId;
}
publicvoid setMsgId(long msgId) {
MsgId = msgId;
}
}
這個類你不需做任何的改動,把包名換成你的就可以了!
請求訊息之文字訊息
package org.liufeng.course.message.req;
/**
* 文字訊息
*
* @author liufeng
* @date 2013-05-19
*/
publicclass TextMessage extends BaseMessage {
// 訊息內容
private String Content;
public String getContent() {
return Content;
}
publicvoid setContent(String content) {
Content = content;
}
}
這個類跟上面的一樣
請求訊息之圖片訊息
package org.liufeng.course.message.req;
/**
* 圖片訊息
*
* @author liufeng
* @date 2013-05-19
*/
publicclass ImageMessage extends BaseMessage {
// 圖片連結
private String PicUrl;
public String getPicUrl() {
return PicUrl;
}
publicvoid setPicUrl(String picUrl) {
PicUrl = picUrl;
}
}
改包名就all了!
請求訊息之地理位置訊息
package org.liufeng.course.message.req;
/**
* 地理位置訊息
*
* @author liufeng
* @date 2013-05-19
*/
publicclass LocationMessage extends BaseMessage {
// 地理位置維度
private String Location_X;
// 地理位置經度
private String Location_Y;
// 地圖縮放大小
private String Scale;
// 地理位置資訊
private String Label;
public String getLocation_X() {
return Location_X;
}
publicvoid setLocation_X(String location_X) {
Location_X = location_X;
}
public String getLocation_Y() {
return Location_Y;
}
publicvoid setLocation_Y(String location_Y) {
Location_Y = location_Y;
}
public String getScale() {
return Scale;
}
publicvoid setScale(String scale) {
Scale = scale;
}
public String getLabel() {
return Label;
}
publicvoid setLabel(String label) {
Label = label;
}
}
同上
請求訊息之連結訊息
package org.liufeng.course.message.req;
/**
* 連結訊息
*
* @author liufeng
* @date 2013-05-19
*/
publicclass LinkMessage extends BaseMessage {
// 訊息標題
private String Title;
// 訊息描述
private String Description;
// 訊息連結
private String Url;
public String getTitle() {
return Title;
}
publicvoid setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
publicvoid setDescription(String description) {
Description = description;
}
public String getUrl() {
return Url;
}
publicvoid setUrl(String url) {
Url = url;
}
}
請求訊息之語音訊息
package org.liufeng.course.message.req;
/**
* 音訊訊息
*
* @author liufeng
* @date 2013-05-19
*/
publicclass VoiceMessage extends BaseMessage {
// 媒體ID
private String MediaId;
// 語音格式
private String Format;
public String getMediaId() {
return MediaId;
}
publicvoid setMediaId(String mediaId) {
MediaId = mediaId;
}
public String getFormat() {
return Format;
}
publicvoid setFormat(String format) {
Format = format;
}
}
到這裡請求訊息的基類就完成了。
原作者: 柳峰