微信公众号Java开发记录(七)回复音乐消息

一、pom依赖

    <!-- 将xml包解析成Map -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <!-- 将Map解析成XML包 -->
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.11.1</version>
        </dependency>

二、实体类代码

xml数据包格式
在这里插入图片描述
在这里插入图片描述
注意:
1.缩略图的媒体id参数参数 是通过素材管理中的接口上传的多媒体文件 得到的id;
2.MusicUrl 自己现在浏览器打开,看能不能播放,我是将音乐文件(mp3格式)上传到了FastDFS,返回的路径可以在浏览器打开可以播放。
3.注意 参数名是xml形式中的名字,下面参数解释MusicUrl写成的是MusicURL,这样也是解析不了的,要按照xml中的名字来写。
BUG
上面的所有参数最好都要写,我就是因为 以为HQMusicUrl这个参数不是必须的,所以没写,就导致 公众号解析不了,一直返回”该公众号故障“。

Music

@Data
public class Music {
    
    
    /**
     * 音乐标题
     */
    @XStreamAlias("Title")
    private String title ;
    /**
     * 音乐描述
     */
    @XStreamAlias("Description")
    private String description;
    /**
     * 音乐连接
     */
    @XStreamAlias("MusicUrl")
    private String musicURL;
    /**
     * 高质量音乐链接,WIFI环境优先使用该链接播放音乐
     */
    @XStreamAlias("HQMusicUrl")
    private String hQMusicUrl;
    /**
     * 缩略图的媒体id,通过素材管理中的接口上传多媒体文件,得到的id
     */
    @XStreamAlias("ThumbMediaId")
    private String thumbMediaId;
}

BaseMessage

@Data
@XStreamAlias("xml")
public class BaseMessage {
    
    
    @XStreamAlias("ToUserName")
    private String toUserName;
    @XStreamAlias("FromUserName")
    private String fromUserName;
    @XStreamAlias("CreateTime")
    private String createTime;
    @XStreamAlias("MsgType")
    private String msgType;
    @XStreamAlias("MsgId")
    private String msgId;


    public BaseMessage(Map<String ,String> map){
    
    
        this.fromUserName=map.get("ToUserName");
        this.toUserName=map.get("FromUserName");
        this.createTime = System.currentTimeMillis()/1000+"";
    }
    public BaseMessage(){
    
    

    }
}

MusicMessage

@Data
@XStreamAlias("xml")
public class MusicMessage extends BaseMessage{
    
    
    @XStreamAlias("Music")
    private Music music;

    public MusicMessage(Map<String ,String> map,Music music){
    
    
        super(map);
        this.setMsgType("music");
        this.music = music;
    }

    public MusicMessage(){
    
    

    }
}

在这里插入图片描述

效果在这里插入图片描述

BUG
上传的缩略图并没有显示出来。
待解决。

猜你喜欢

转载自blog.csdn.net/DreamsArchitects/article/details/109220553