2-5 音乐消息的回复

 

/Weixin/src/com/imooc/test/WeixinTest.java

修改 

package com.imooc.test;

import com.imooc.po.AccessToken;
import com.imooc.util.WeixinUtil;

public class WeixinTest {

	public static void main(String[] args) {
		AccessToken token = WeixinUtil.getAccessToken();
		System.out.println("票据:"+token.getToken());
		System.out.println("有效时间:"+token.getExpiresIn());
		
		String path = "G:/imooc.jpg";
		try {
			
			String mediaId = WeixinUtil.upload(path, token.getToken(), "image");
			System.out.println(mediaId);
		
		
		}  catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

把 image 改成:thumb

执行类

报错:

Exception in thread "main" net.sf.json.JSONException: JSONObject["access_token"] not found.
	at net.sf.json.JSONObject.getString(JSONObject.java:2245)
	at com.imooc.util.WeixinUtil.getAccessToken(WeixinUtil.java:90)
	at com.imooc.test.WeixinTest.main(WeixinTest.java:9)

因为 需要重新百度一下,把IP添加到白名单里

然后再执行,得到如下

票据:12_rfbnSQ4W2IrDa2gn4Bph32l4wRtYvnv0F4XeUsd2F1yXCSsU0KuMcNrNbeIAavj89EELJzlgMzPXhdt25TmsAu9vIokJ0ylai2hcSLlyuTcH5XYRKMhtI6G8dpKY-yWryb08Zqq4eURJKNR_MFWhABADWN
有效时间:7200
{"type":"thumb","thumb_media_id":"YbmOPlJPhfliKtylXeLGg0QdOl308xnGpY2GJBsGSYYZWpksSQbHIAMGK2YDv7ea","created_at":1534259782}
YbmOPlJPhfliKtylXeLGg0QdOl308xnGpY2GJBsGSYYZWpksSQbHIAMGK2YDv7ea

我们拿到缩略图的 thumb_media_id 以后 就可以音乐消息的组装了。 

注意:我发现,每执行一次,thumb_media_id 都不一样。

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140543

 

建实体类

/Weixin/src/com/imooc/po/Music.java

package com.imooc.po;

public class Music {
	private String Title;
	private String Description;
	private String MusicURL;
	private String HQMusicUrl;
	private String ThumbMediaId;
	
	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 hQMusicUrl) {
		HQMusicUrl = hQMusicUrl;
	}
	public String getThumbMediaId() {
		return ThumbMediaId;
	}
	public void setThumbMediaId(String thumbMediaId) {
		ThumbMediaId = thumbMediaId;
	}
	
	
	
}

 

/Weixin/src/com/imooc/po/MusicMessage.java

package com.imooc.po;

public class MusicMessage extends BaseMessage{
	private Music Music;

	public Music getMusic() {
		return Music;
	}

	public void setMusic(Music music) {
		Music = music;
	}
	
	
}

 

/Weixin/src/com/imooc/util/MessageUtil.java

	/**
	 * 音乐消息转化为 xml
	 * @param musicMessage
	 * @return
	 */
	public static String musicMessageToXml(MusicMessage musicMessage){
		
		XStream xstream = new XStream();
		xstream.alias("xml", musicMessage.getClass());
		
		return xstream.toXML(musicMessage);
	}

	/**
	 * 音乐消息组装
	 * @param toUserName
	 * @param fromUserName
	 * @return
	 */
	public static String initMusicMessage(String toUserName,String fromUserName){
		String message = null;
		Music music = new Music();
		music.setThumbMediaId("rxMbsUtUcix11Ct7MaNJJLvTORDtsIZT5NAfK95kn5Sy7iF2lnIg-PMlticjC38Q");
		music.setTitle("不知道");
		music.setDescription("真的不知道");
		music.setMusicURL("http://huanglianggu.s1.natapp.cc/Weixin/source/music.mp3");
		music.setHQMusicUrl("http://huanglianggu.s1.natapp.cc/Weixin/source/music.mp3");
		
		MusicMessage musicMessage = new MusicMessage();
		musicMessage.setFromUserName(toUserName);
		musicMessage.setToUserName(fromUserName);
		musicMessage.setMsgType(MESSAGE_MUSIC);
		musicMessage.setCreateTime(new Date().getTime());
		musicMessage.setMusic(music);
		message = musicMessageToXml(musicMessage);
		return message;
	}

 

/Weixin/src/com/imooc/test/WeixinTest.java

package com.imooc.test;

import com.imooc.po.AccessToken;
import com.imooc.util.WeixinUtil;

public class WeixinTest {

	public static void main(String[] args) {
		AccessToken token = WeixinUtil.getAccessToken();
		System.out.println("票据:"+token.getToken());
		System.out.println("有效时间:"+token.getExpiresIn());
		
		String path = "G:/imooc.jpg";
		try {
			
			String mediaId = WeixinUtil.upload(path, token.getToken(), "thumb");
			System.out.println(mediaId);
		
		
		}  catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

//票据:12_X5z1aoeGCe9KvRizvGlZENReaU938o-ZAyfKvsyu1247RnBI0Gn29zapqW12M5qkXEzYA9M2QmDBwIMgUjT5FAvnZvtigI4W4wLRVSFOTbgY5pIP7Wz93h5aMKTWqNCDEeMVj-1wZPm5wVJMHZHhAFARJR
//有效时间:7200
//{"type":"image","media_id":"o6k7h1lwDwu0Btjp741WGSRqNhUso8aW1iM5bziSmO9uaalXnBVVmGanNgY8Ggpv","created_at":1534213347}
//o6k7h1lwDwu0Btjp741WGSRqNhUso8aW1iM5bziSmO9uaalXnBVVmGanNgY8Ggpv


//票据:12_rfbnSQ4W2IrDa2gn4Bph32l4wRtYvnv0F4XeUsd2F1yXCSsU0KuMcNrNbeIAavj89EELJzlgMzPXhdt25TmsAu9vIokJ0ylai2hcSLlyuTcH5XYRKMhtI6G8dpKY-yWryb08Zqq4eURJKNR_MFWhABADWN
//有效时间:7200
//{"type":"thumb","thumb_media_id":"YbmOPlJPhfliKtylXeLGg0QdOl308xnGpY2GJBsGSYYZWpksSQbHIAMGK2YDv7ea","created_at":1534259782}
//YbmOPlJPhfliKtylXeLGg0QdOl308xnGpY2GJBsGSYYZWpksSQbHIAMGK2YDv7ea

 

 

				}else if("4".equals(content)){
					message = MessageUtil.initMusicMessage(toUserName, fromUserName);
				}else if("?".equals(content) || "?".equals(content)){

 

 

在文件:/Weixin/src/com/imooc/util/MessageUtil.java

把原来的music.setMusicURL("http://huanglianggu.s1.natapp.cc/Weixin/source/music.mp3");

修改为:music.setMusicUrl("http://huanglianggu.s1.natapp.cc/Weixin/source/music.mp3");

不然,老是提示:该公众号提供的服务出现故障,请稍后再试。

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值