微信--上传图文消息 群发消息

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baoping.commons.util.HttpUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class WxMassMsg {
//上传图文消息素材

    public String getUploadMsg(String access_token,HashMap map) throws IOException {
        String media_id = null;
        String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token="
                + access_token;
        String json = JSON.toJSONString(map);
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        StringEntity params = new StringEntity(json,"UTF-8");
        httpPost.setEntity(params);
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = client.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();
            String jsonString = EntityUtils.toString(entity);
            JSONObject fromObject = JSONObject.parseObject(jsonString);
            Object media_idObject = fromObject.get("media_id");
            if (media_idObject != null) {
                media_id = media_idObject.toString();
            }
            System.out.println("上传图文消息素材-返回值:"
                    + jsonString);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(httpResponse != null){
                httpResponse.close();
            }
            if(client != null){
                client.close();
            }
        }
        return media_id;
    }

    // 根据分组进行群发
    public String getMassMsg(String access_token,HashMap map) throws IOException {
        String url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+access_token;

        String json = JSON.toJSONString(map);
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        StringEntity params = new StringEntity(json,"UTF-8");
        httpPost.setEntity(params);
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = client.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();
            String jsonString = EntityUtils.toString(entity);
            System.out.println("群发消息-返回值:"
                    + jsonString);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(httpResponse != null){
                httpResponse.close();
            }
            if(client != null){
                client.close();
            }
        }
        return null;

    }


    public static void main(String[] args){
        String appId = "";
        String secret = "";
        WeixinApi weixinApi = new WeixinApi(appId,secret);

        HashMap mapUp = new HashMap();
        HashMap SubMap = new HashMap();
        List list = new ArrayList();
        SubMap.put("thumb_media_id", "");
        SubMap.put("author", "aaaaa");
        SubMap.put("title", "bbbbb");
        SubMap.put("content_source_url","www.cccc.com");
        SubMap.put("content", "ddddd");
        SubMap.put("digest", "eeee");
        SubMap.put("show_cover_pic", "0");
        list.add(SubMap);
        mapUp.put("articles", list);

        String mediaId = null;
        WxMassMsg wxMassMsg = new WxMassMsg();
        try {
            mediaId = wxMassMsg.getUploadMsg(weixinApi.getAccessToken(), mapUp);
            System.out.println(mediaId);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("出错了");
        }


        HashMap msgMap1 = new HashMap();
        HashMap msgMap2 = new HashMap();
        HashMap msgMap3 = new HashMap();
        msgMap2.put("is_to_all",true);
        msgMap3.put("media_id",mediaId);
        msgMap1.put("filter",msgMap2);
        msgMap1.put("mpnews",msgMap3);
        msgMap1.put("msgtype","mpnews");
        try {
            wxMassMsg.getMassMsg(weixinApi.getAccessToken(), msgMap1);
            System.out.println("完成");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
 
之前我的SubMap.put("show_cover_pic", "1");出现错误{errcode": -2, "errmsg": ""}改为0后解决了,但不知道为什么。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值