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

  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为错误信息

  具体api可查看:http://mp.weixin.qq.com/wiki/index.php?title=%E9%AB%98%E7%BA%A7%E7%BE%A4%E5%8F%91%E6%8E%A5%E5%8F%A3

二、关于java代码的调用

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

三、代码实现

 package com.demo.test;

 import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser; public class Test
{
public static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";// 获取access
public static final String UPLOAD_IMAGE_URL = "http://file.api.weixin.qq.com/cgi-bin/media/upload";// 上传多媒体文件
public static final String UPLOAD_FODDER_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
public static final String GET_USER_GROUP = "https://api.weixin.qq.com/cgi-bin/groups/get"; // url
public static final String SEND_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
public static final String APP_ID = "wxa549b28c24cf341e";
public static final String SECRET = "78d8a8cd7a4fa700142d06b96bf44a37"; /**
* 发送消息
*
* @param uploadurl
* apiurl
* @param access_token
* @param data
* 发送数据
* @return
*/
public static String sendMsg(String url, String token, String data)
{
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
String sendurl = String.format("%s?access_token=%s", url, token);
PostMethod post = new UTF8PostMethod(sendurl);
post
.setRequestHeader(
"User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0"); post.setRequestHeader("Host", "file.api.weixin.qq.com");
post.setRequestHeader("Connection", "Keep-Alive");
post.setRequestHeader("Cache-Control", "no-cache");
String result = null;
try
{
post.setRequestBody(data);
int status = client.executeMethod(post);
if (status == HttpStatus.SC_OK)
{
String responseContent = post.getResponseBodyAsString();
System.out.println(responseContent);
JsonParser jsonparer = new JsonParser();// 初始化解析json格式的对象
JsonObject json = jsonparer.parse(responseContent)
.getAsJsonObject();
if (json.get("errcode") != null
&& json.get("errcode").getAsString().equals("0"))
{
result = json.get("errmsg").getAsString();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
return result;
}
} public static class UTF8PostMethod extends PostMethod {//防止发送的消息产生乱码
public UTF8PostMethod(String url) {
super(url);
} @Override
public String getRequestCharSet() {//文本编码格式
return "UTF-8";
}
} public static void main(String[] args) throws Exception
{
String accessToken = getToken(GET_TOKEN_URL, APP_ID, SECRET);// 获取token在微信接口之一中获取
if (accessToken != null)// token成功获取
{
System.out.println(accessToken);
File file = new File("f:" + File.separator + "2000.JPG"); // 获取本地文件
String id = uploadImage(UPLOAD_IMAGE_URL, accessToken, "image",
file);// java微信接口之三—上传多媒体文件 可获取
if (id != null)
{
// 构造数据
Map map = new HashMap();
map.put("thumb_media_id", id);
map.put("author", "wxx");
map.put("title", "标题");
map.put("content", "测试fdsfdsfsdfssfdsfsdfsdfs");
map.put("digest", "digest");
map.put("show_cover_pic", "0"); Map map2 = new HashMap();
map2.put("thumb_media_id", id);
map2.put("author", "wxx");
map2.put("content_source_url", "www.google.com");
map2.put("title", "标题");
map2.put("content", "测试fdsfdsfsdfssfdsfsdfsdfs");
map2.put("digest", "digest"); Map map3 = new HashMap();
List<Map> list = new ArrayList<Map>();
list.add(map);
list.add(map2);
map3.put("articles", list); Gson gson = new Gson();
String result = gson.toJson(map3);// 转换成json数据格式
String mediaId = uploadFodder(UPLOAD_FODDER_URL, accessToken,
result);
if (mediaId != null)
{
String ids = getGroups(GET_USER_GROUP, accessToken);// 在java微信接口之二—获取用户组
if (ids != null)
{
String[] idarray = ids.split(",");// 用户组id数组
for (String gid : idarray)
{ JsonObject jObj = new JsonObject();
JsonObject filter = new JsonObject();
filter.addProperty("group_id", gid);
jObj.add("filter", filter); JsonObject mpnews = new JsonObject();
mpnews.addProperty("media_id", mediaId);
jObj.add("mpnews", mpnews); jObj.addProperty("msgtype", "mpnews");
System.out.println(jObj.toString()); String result2 = sendMsg(SEND_MESSAGE_URL,
accessToken, jObj.toString());
System.out.println(result2);
}
}
} }
}
}
}

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

java微信接口之五—消息分组群发的更多相关文章

  1. java微信接口之四—上传素材

    一.微信上传素材接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=A ...

  2. java 微信自定义菜单 java微信接口开发 公众平台 SSM redis shiro 多数据源

    A 调用摄像头拍照,自定义裁剪编辑头像,头像图片色度调节B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,快速开发利器)+快速表单构建器 freemaker模版技术 ,0个代码不用写,生成 ...

  3. java微信接口之三—上传多媒体文件

    一.微信上传多媒体接口简介 1.请求:该请求是使用post提交from来实现的,我们可以在网页上进行表单提交来实现.地址为: http://file.api.weixin.qq.com/cgi-bin ...

  4. java访问微信接口发送消息

    最近在开发activiti流程的时候有个需求:流程到达每个审批节点后,需要向该节点的审批人发送一个消息,提示有审批需要处理. 参考了一下微信的开发者文档和网络上的一些技术博客,现在记录一下.以便后续继 ...

  5. java微信接口之二—获取用户组

    一.微信获取用户组接口简介 1.请求 该请求也是GET方式请求.请求的url格式如下: https://api.weixin.qq.com/cgi-bin/groups/get?access_toke ...

  6. java微信接口之——获取access_token

    本文转自http://www.cnblogs.com/always-online/category/598553.html 一.微信获取access_token接口简介 1.请求:该请求是GET方式请 ...

  7. 微信分组群发45028,微信分组群发has no masssend quota hint

    微信分组群发45028,微信分组群发has no masssend quota hint >>>>>>>>>>>>>> ...

  8. 微信分组群发图文40152,微信分组群发图文invalid group id hint

    微信分组群发40152,微信分组群发invalid group id hint >>>>>>>>>>>>>>> ...

  9. C#开发微信门户及应用(30)--消息的群发处理和预览功能

    在很多场合下,我们可能需要利用微信公众号的优势,定期给指定用户群发送一些推广消息或者新闻内容,以便给关注客户一种经常更新公众号内容的感觉,同时也方便我们经常和用户进行互动.微信公众号的高级群发接口就是 ...

随机推荐

  1. 一篇通俗易懂的讲解OpenGL ES的文章

    电脑或者手机上做图像处理有很多方式,但是目前为止最高效的方法是有效地使用图形处理单元,或者叫 GPU.你的手机包含两个不同的处理单元,CPU 和 GPU.CPU 是个多面手,并且不得不处理所有的事情, ...

  2. 如何转移数据库MDF和LDF文件

    我们可以很轻易地使用SQL Server来创建一个数据库,创建的数据库实例将存储在指定的默认位置(不一定是C盘,可以手动变更默认存储位置).假设此时数据库实例创建在了C盘中的默认位置,亦即是与数据库安 ...

  3. MVC怎么在当前视图中,传递参数给到另外一个视图?

    在TransData.cshtml视图中: <div> <!--在一个视图中,请求另外一个视图,并且将数据传到另外一个视图--> <!--视图中调用无返回值的方法,需要加 ...

  4. SQL Server存储过程复习(一)

    --存储过程学习篇 --.简单存储过程不带参数的学习 IF OBJECT_ID('Orders_GetAllOrders','P') IS NOT NULL DROP PROCEDURE Orders ...

  5. Unity多语言本地化改进版

    简介 之前捣鼓过一个通过csv配置游戏多语言支持的小工具,但是发现使用过程中,通过notepad++去进行转码很不方便,并且直接将配置的csv不加密的放在游戏中心里感觉不是很踏实 于是乎~~ 新的方案 ...

  6. 安装、部署... Windows服务 .net程序 安装 命令

    @echo offInstallutil.exe 程序目录 F:\test\TestWindows.exe 服务程序目录@sc start "服务名称"@sc config &qu ...

  7. SQL Server 2008 评估期已过解决方法

    SQL Server 2008有180天的试用期,过期后会提示“评估期已过”的提示. 1.进入SQL Server安装中心: 2.选择“维护”-“版本升级” 3.输入密钥: 其他的根据提示操作. 附S ...

  8. Python入门笔记(13):列表解析

    一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter_var in iterable] [expr for iter_var in iterable i ...

  9. 2 Orchard汉化资源包的使用

    Orchard安装完毕之后我们就可以在后台尝试做一些基本的操作感受下Orchard提供的一些功能,比如添加一个页面.菜单.文章什么的.也可以试着新建一些部件.布局之类的感受下.个人建议摆弄一下了解下就 ...

  10. Hibernate Validator

    http://docs.jboss.org/hibernate/validator/4.2/reference/zh-CN/html_single/#example-group-interfaces