根据OpenID列表群发 高级群发消息

package com.wanhua.weixin.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import play.cache.Cache;
import play.i18n.Messages;

import com.alibaba.fastjson.JSON;
import com.wanhua.util.Const;
import com.wanhua.weixin.util.WXConst;
import com.wanhua.weixin.util.WXHttpUtil;

/**
 * 根据OpenID列表群发 高级群发消息
 *
 * @author w_xfpenga
 *
 *         2014-11-27
 */
public class AdvanceInterfaceMsg {

    // 填写图文消息的接收者,一串OpenID列表,OpenID最少1个,最多10000个
    public List<String> touser;

    // 用于群发的图文消息的mpnews
    public Map<String, Object> mpnews;

    // 群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video
    public String msgtype;

    /**
     * 根据OpenID列表群发的高级群发接口 发送高级群发消息
     *
     * @param wxMsgXml
     */
    public static int SendMsgNewsByOpenId(List<String> touser, String media_id) {
        // 获取到的凭证
        String access_token = AccessToken.getAccessToken();
        // 请求地址
        String requestUrl = WXConst.ADVANCE_INTERFACE_URL + access_token;
        // 上传的文件的ID
        Map<String, Object> mpnews = new HashMap<String, Object>();
        mpnews.put("media_id", media_id);
        // 实例化 高级群发消息对象
        AdvanceInterfaceMsg advanceInterfaceMsg = new AdvanceInterfaceMsg();
        advanceInterfaceMsg.touser = touser;
        advanceInterfaceMsg.mpnews = mpnews;
        advanceInterfaceMsg.msgtype = "mpnews";
        // 请求输出参数
        String outputStr = JSON.toJSONString(advanceInterfaceMsg);
        try {
            // 请求返回的结果
            String result = WXHttpUtil.MsgHttpsRequest(requestUrl, "POST", outputStr);
            // 将返回结果转换成json格式数据
            JSONObject resultObj = new JSONObject(result);
            // 返回的结果的状态码
            int code = resultObj.getInt("errcode");
            if (code == 0) {
                // 请求成功返回提示信息
                System.out.println(" 发送高级群发消息成功" + result);
                return 0;
            } else {
                // 请求失败的返回提示信息
                System.out.println(" 发送高级群发消息失败" + result);
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }

    }

    /**
     * 根据删除高级群发消息
     *
     * @param msg_id
     */
    public static void DeleteAdvanceMsg(String msg_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("msg_id", msg_id);
            // 获取到的凭证
            String access_token = AccessToken.getAccessToken();
            // 请求地址
            String requestUrl = WXConst.DELETE_ADVANCE_URL + access_token;
            // 请求参数
            String outputStr = jsonObject.toString();
            try {
                // 请求返回的结果
                String result = WXHttpUtil.MsgHttpsRequest(requestUrl, "POST", outputStr);
                // 将返回结果转换成json格式数据
                JSONObject resultObj = new JSONObject(result);
                // 返回的结果的状态码
                int code = resultObj.getInt("errcode");
                if (code == 0) {
                    // 请求成功返回提示信息
                    System.out.println(Messages.get("advance_interface_delete_message_success") + result);
                } else {
                    // 请求失败的返回提示信息
                    System.out.println(Messages.get("advance_interface_delete_message_fail") + result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

}

原文地址:https://www.cnblogs.com/xunfang123/p/4237127.html