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后解决了,但不知道为什么。