java 微信高级群发_java微信平台,高级群发接口开发

一、微信消息分组群发接口简介

1、请求:该请求是使用post提交地址为:

https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN

其中ACCESS_TOKEN是我们动态获取的 。

发送的数据:(这里使用图文消息示例)

{

"filter":{

"group_id":"2" },

"mpnews":{

"media_id":"123dsdajkasd231jhksad" },

"msgtype":"mpnews"

}

filter,用于设定图文消息的接收者; group_id,群发到的分组的group_id(可在java微信接口之二—获取用户组);

mpnews,用于设定即将发送的图文消息;

media_id, 用于群发的消息的media_id(可在java微信接口之四—上传素材中获取); msgtype,群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video.

2、响应:该响应也是以json方式返回的

正确的时候返回的数据:

{

"errcode":0,

"errmsg":"send job submission success",

"msg_id":34182

}

errcode 错误码

errmsg 错误信息

msg_id 消息ID

错误的时候返回的数据:{"errcode":40004,"errmsg":"invalid media type"}

errcode,为错误代码,errmsg为错误信息

二、关于java代码的调用

该接口的调用与java微信接口四—上传素材一样,需要使用到commons-httpclient。其中数据是构造成json数据后,放在post方法请求体里面发送给服务器端。

三、代码实现

1 packagecom.demo.test;2

3 importjava.io.File;4 importjava.util.ArrayList;5 importjava.util.HashMap;6 importjava.util.List;7 importjava.util.Map;8

9 importorg.apache.commons.httpclient.methods.PostMethod;10 importorg.apache.commons.httpclient.methods.multipart.FilePart;11 importorg.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;12 importorg.apache.commons.httpclient.methods.multipart.Part;13 importorg.apache.http.HttpEntity;14 importorg.apache.http.HttpResponse;15 importorg.apache.http.HttpStatus;16 importorg.apache.http.client.HttpClient;17 importorg.apache.http.client.methods.HttpGet;18 importorg.apache.http.impl.client.DefaultHttpClient;19 importorg.apache.http.util.EntityUtils;20

21 importcom.google.gson.Gson;22 importcom.google.gson.JsonArray;23 importcom.google.gson.JsonObject;24 importcom.google.gson.JsonParser;25

26 public classTest27 {28 public static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";//获取access

29 public static final String UPLOAD_IMAGE_URL = "http://file.api.weixin.qq.com/cgi-bin/media/upload";//上传多媒体文件

30 public static final String UPLOAD_FODDER_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";31 public static final String GET_USER_GROUP = "https://api.weixin.qq.com/cgi-bin/groups/get"; //url

32 public static final String SEND_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";33 public static final String APP_ID = "wxa549b28c24cf341e";34 public static final String SECRET = "78d8a8cd7a4fa700142d06b96bf44a37";35

36

37 /**38 * 发送消息39 *40 *@paramuploadurl41 * apiurl42 *@paramaccess_token43 *@paramdata44 * 发送数据45 *@return

46 */

47 public staticString sendMsg(String url, String token, String data)48 {49 org.apache.commons.httpclient.HttpClient client = neworg.apache.commons.httpclient.HttpClient();50 String sendurl = String.format("%s?access_token=%s", url, token);51 PostMethod post = newPostMethod(sendurl);52 post53 .setRequestHeader(54 "User-Agent",55 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0");56

57 post.setRequestHeader("Host", "file.api.weixin.qq.com");58 post.setRequestHeader("Connection", "Keep-Alive");59 post.setRequestHeader("Cache-Control", "no-cache");60 String result = null;61 try

62 {63 post.setRequestBody(data);64 int status =client.executeMethod(post);65 if (status ==HttpStatus.SC_OK)66 {67 String responseContent =post.getResponseBodyAsString();68 System.out.println(responseContent);69 JsonParser jsonparer = new JsonParser();//初始化解析json格式的对象

70 JsonObject json =jsonparer.parse(responseContent)71 .getAsJsonObject();72 if (json.get("errcode") != null

73 && json.get("errcode").getAsString().equals("0"))74 {75 result = json.get("errmsg").getAsString();76 }77 }78 }79 catch(Exception e)80 {81 e.printStackTrace();82 }83 finally

84 {85 returnresult;86 }87 }88

89 public static void main(String[] args) throwsException90 {91 String accessToken = getToken(GET_TOKEN_URL, APP_ID, SECRET);//获取token在微信接口之一中获取

92 if (accessToken != null)//token成功获取

93 {94 System.out.println(accessToken);95 File file = new File("f:" + File.separator + "2000.JPG"); //获取本地文件

96 String id = uploadImage(UPLOAD_IMAGE_URL, accessToken, "image",97 file);//java微信接口之三—上传多媒体文件 可获取

98 if (id != null)99 {100 //构造数据

101 Map map = newHashMap();102 map.put("thumb_media_id", id);103 map.put("author", "wxx");104 map.put("title", "标题");105 map.put("content", "测试fdsfdsfsdfssfdsfsdfsdfs");106 map.put("digest", "digest");107 map.put("show_cover_pic", "0");108

109 Map map2 = newHashMap();110 map2.put("thumb_media_id", id);111 map2.put("author", "wxx");112 map2.put("content_source_url", "www.google.com");113 map2.put("title", "标题");114 map2.put("content", "测试fdsfdsfsdfssfdsfsdfsdfs");115 map2.put("digest", "digest");116

117 Map map3 = newHashMap();118 List list = new ArrayList();119 list.add(map);120 list.add(map2);121 map3.put("articles", list);122

123 Gson gson = newGson();124 String result = gson.toJson(map3);//转换成json数据格式

125 String mediaId =uploadFodder(UPLOAD_FODDER_URL, accessToken,126 result);127 if (mediaId != null)128 {129 String ids = getGroups(GET_USER_GROUP, accessToken);//在java微信接口之二—获取用户组

130 if (ids != null)131 {132 String[] idarray = ids.split(",");//用户组id数组

133 for(String gid : idarray)134 {135

136 JsonObject jObj = newJsonObject();137 JsonObject filter = newJsonObject();138 filter.addProperty("group_id", gid);139 jObj.add("filter", filter);140

141

142 JsonObject mpnews = newJsonObject();143 mpnews.addProperty("media_id", mediaId);144 jObj.add("mpnews", mpnews);145

146 jObj.addProperty("msgtype", "mpnews");147 System.out.println(jObj.toString());148

149 String result2 =sendMsg(SEND_MESSAGE_URL,150 accessToken, jObj.toString());151 System.out.println(result2);152 }153 }154 }155

156 }157 }158 }159 }

发成功后会打印消息,但由于微信服务器的原因,消息不会立即发送,会过一段时间发送。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值