JAVA微信开发:[20]如何实现主动发送微信消息
2015-07-08 15:58
1456 查看
在微信开发中,利用微信的接口主动给微信关注用户发送消息。这个主要是利用微信的客服接口来实现该功能,具体的实现如下:
/**
* 微信公共账号发送给账号
* @param content 文本内容
* @param toUser 微信用户
* @return
*/
public void sendTextMessageToUser(String content,String toUser){
String json = "{\"touser\": \""+toUser+"\",\"msgtype\": \"text\", \"text\": {\"content\": \""+content+"\"}}";
//获取access_token
GetExistAccessToken getExistAccessToken = GetExistAccessToken.getInstance();
String accessToken = getExistAccessToken.getExistAccessToken();
//获取请求路径
String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
System.out.println("json:"+json);
try {
connectWeiXinInterface(action,json);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 微信公共账号发送给账号(本方法限制使用的消息类型是语音或者图片)
* @param mediaId 图片或者语音内容
* @param toUser 微信用户
* @param messageType 消息类型
* @return
*/
public void sendPicOrVoiceMessageToUser(String mediaId,String toUser,String msgType){
String json=null;
if(msgType.equals(REQ_MESSAGE_TYPE_IMAGE)){
json = "{\"touser\": \""+toUser+"\",\"msgtype\": \"image\", \"image\": {\"media_id\": \""+mediaId+"\"}}";
}else if(msgType.equals(REQ_MESSAGE_TYPE_VOICE)){
json = "{\"touser\": \""+toUser+"\",\"msgtype\": \"voice\", \"voice\": {\"media_id\": \""+mediaId+"\"}}";
}
//获取access_token
GetExistAccessToken getExistAccessToken = GetExistAccessToken.getInstance();
String accessToken = getExistAccessToken.getExistAccessToken();
//获取请求路径
String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
try {
connectWeiXinInterface(action,json);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送图文给所有的用户
* @param openId 用户的id
*/
public void sendNewsToUser(String openId){
MediaUtil mediaUtil = MediaUtil.getInstance();
ArrayList<Object> articles = new ArrayList<Object>();
Article a = new Article();
articles.add(a);
String str = JsonUtil.getJsonStrFromList(articles);
String json = "{\"touser\":\""+openId+"\",\"msgtype\":\"news\",\"news\":" +
"{\"articles\":" +str +"}"+"}";
json = json.replace("picUrl", "picurl");
System.out.println(json);
//获取access_token
String access_token = mediaUtil.getAccess_token();
//获取请求路径
String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+access_token;
try {
connectWeiXinInterface(action,json);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 连接请求微信后台接口
* @param action 接口url
* @param json 请求接口传送的json字符串
*/
public void connectWeiXinInterface(String action,String json){
URL url;
try {
url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
OutputStream os = http.getOutputStream();
os.write(json.getBytes("UTF-8"));// 传入参数
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String result = new String(jsonBytes, "UTF-8");
System.out.println("请求返回结果:"+result);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
转载地址:http://jingyan.baidu.com/article/b907e627b1e4bb46e7891cf0.html
/**
* 微信公共账号发送给账号
* @param content 文本内容
* @param toUser 微信用户
* @return
*/
public void sendTextMessageToUser(String content,String toUser){
String json = "{\"touser\": \""+toUser+"\",\"msgtype\": \"text\", \"text\": {\"content\": \""+content+"\"}}";
//获取access_token
GetExistAccessToken getExistAccessToken = GetExistAccessToken.getInstance();
String accessToken = getExistAccessToken.getExistAccessToken();
//获取请求路径
String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
System.out.println("json:"+json);
try {
connectWeiXinInterface(action,json);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 微信公共账号发送给账号(本方法限制使用的消息类型是语音或者图片)
* @param mediaId 图片或者语音内容
* @param toUser 微信用户
* @param messageType 消息类型
* @return
*/
public void sendPicOrVoiceMessageToUser(String mediaId,String toUser,String msgType){
String json=null;
if(msgType.equals(REQ_MESSAGE_TYPE_IMAGE)){
json = "{\"touser\": \""+toUser+"\",\"msgtype\": \"image\", \"image\": {\"media_id\": \""+mediaId+"\"}}";
}else if(msgType.equals(REQ_MESSAGE_TYPE_VOICE)){
json = "{\"touser\": \""+toUser+"\",\"msgtype\": \"voice\", \"voice\": {\"media_id\": \""+mediaId+"\"}}";
}
//获取access_token
GetExistAccessToken getExistAccessToken = GetExistAccessToken.getInstance();
String accessToken = getExistAccessToken.getExistAccessToken();
//获取请求路径
String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
try {
connectWeiXinInterface(action,json);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送图文给所有的用户
* @param openId 用户的id
*/
public void sendNewsToUser(String openId){
MediaUtil mediaUtil = MediaUtil.getInstance();
ArrayList<Object> articles = new ArrayList<Object>();
Article a = new Article();
articles.add(a);
String str = JsonUtil.getJsonStrFromList(articles);
String json = "{\"touser\":\""+openId+"\",\"msgtype\":\"news\",\"news\":" +
"{\"articles\":" +str +"}"+"}";
json = json.replace("picUrl", "picurl");
System.out.println(json);
//获取access_token
String access_token = mediaUtil.getAccess_token();
//获取请求路径
String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+access_token;
try {
connectWeiXinInterface(action,json);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 连接请求微信后台接口
* @param action 接口url
* @param json 请求接口传送的json字符串
*/
public void connectWeiXinInterface(String action,String json){
URL url;
try {
url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
OutputStream os = http.getOutputStream();
os.write(json.getBytes("UTF-8"));// 传入参数
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String result = new String(jsonBytes, "UTF-8");
System.out.println("请求返回结果:"+result);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
转载地址:http://jingyan.baidu.com/article/b907e627b1e4bb46e7891cf0.html
相关文章推荐
- 二维码小程序
- 微信支付
- 可穿戴设备(CC2541)上微信(3)
- 微信企业号开启回调模式的时候,错误:echostr校验失败,请您检查是否正确解密并输出明文
- 微信公众号第三方平台全网发布测试程序
- 小商家为何要逃离微信公众平台
- [一车大师]微信运营心得
- [一车大师]微信公众平台开发入门
- C#/ASP.NET MVC微信公众号接口开发之从零开发(一) 接入微信公众平台
- 基于nodejs 的微信 JS-SDK 简单应用
- Java 基本常识+概述+环境变量配置+Hello World小程序
- Android第四十二期 - 关于微信手势退出Activity的操作
- Android第四十二期 - 关于微信手势退出Activity的操作
- 微信移动用户他入组
- 如何判断微信内置浏览器
- 微信公众平台网页获取用户OpenID方法
- 微信支付v3发布到iis时的证书问题(转)
- 可穿戴设备(CC2541)上微信(2)
- 2015年微信公众号媒体价值研究报告
- ecshop第三方登陆之微信登录