java微信发送图文和文字消息

1、获取token

        public static Map getAccessToken(WxConstant wxConstant) {
// 第二步获取有效的access_token
try {
StringBuffer sb = new StringBuffer(wxConstant.wxServer);
sb.append("cgi-bin/token?");
sb.append("grant_type=client_credential");
sb.append("&appid=");
sb.append(wxConstant.wxAppId);
sb.append("&secret=");
sb.append(wxConstant.wxSecret);


String result = HttpClientUtil.get(sb.toString(), null);
System.out.println(result);
JSONObject json = JSONObject.fromObject(result);


if (json.containsKey("errcode")) {
String errcode = json.get("errcode").toString();
String errmsg = json.get("errmsg").toString();
Map map = new HashMap();
  map.put("code", "001");
map.put("message", errcode + "," + errmsg);
return map;


} else {
String accessToken = json.get("access_token").toString();
Map map = new HashMap();
map.put("result", result);
map.put("code", "000");
map.put("accessToken", accessToken);
map.put("message", "success");
return map;
}
} catch (Exception e) {
e.printStackTrace();
Map map = new HashMap();
map.put("code", "001");
map.put("message", e.getMessage());
return map;
}
}



2、创建需要发送的数据

  //创建发送的数据 文字消息
    private static String createSendDataToText(String openId,Map map){
         JSONObject gjson =new JSONObject();
          gjson.put("touser", openId);
          gjson.put("msgtype", "text");
          
         JSONObject text =new JSONObject();
         text.put("content", String.valueOf(map.get("content")));
         gjson.put("text", text);
         
         
        return gjson.toString();
    }


    //图文消息数据
    private static String createSendData(String openId,Map map){
         JSONObject gjson =new JSONObject();
          gjson.put("touser", openId);
         gjson.put("msgtype", "news");
         JSONObject news =new JSONObject();
         JSONArray articles =new JSONArray();
         JSONObject list =new JSONObject();
         list.put("title",String.valueOf(map.get("title"))); //标题
         list.put("description",String.valueOf(map.get("description"))); //描述
         list.put("url",String.valueOf(map.get("url"))); //点击图文链接跳转的地址
         list.put("picurl",String.valueOf(map.get("picurl"))); //图文链接的图片
         articles.add(list);
         news.put("articles", articles);
         JSONObject text =new JSONObject();
         text.put("test1", String.valueOf(map.get("test1")));
         gjson.put("text", text);
         gjson.put("news", news);
         
        return gjson.toString();
    }


3、发送消息

public static void testsendTextsByOpenids(String openId,Map map,WxConstant wxConstant){
        String urlstr =wxConstant.wxServer+"cgi-bin/message/custom/send?access_token=ACCESS_TOKEN"; //发送客服图文消息
        Map accessTokenMap = WeixinUtil.getAccessToken(wxConstant);
  String accessToken = accessTokenMap.get("accessToken");
        urlstr =urlstr.replace("ACCESS_TOKEN", accessToken);
        System.out.println("urlstr:"+urlstr);
          String reqjson =createSendDataToText(openId,map);
         try {
             URL httpclient =new URL(urlstr);
             HttpURLConnection conn =(HttpURLConnection) httpclient.openConnection();
             conn.setConnectTimeout(5000);
             conn.setReadTimeout(2000);
             conn.setRequestMethod("POST");
             conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");    
             conn.setDoOutput(true);        
             conn.setDoInput(true);
             conn.connect();
             OutputStream os= conn.getOutputStream();    
             System.out.println("ccccc:"+reqjson);
             os.write(reqjson.getBytes("UTF-8"));//传入参数    
             os.flush();
             os.close();
             
             InputStream is =conn.getInputStream();
             int size =is.available();
             byte[] jsonBytes =new byte[size];
             is.read(jsonBytes);
             String message=new String(jsonBytes,"UTF-8");
             System.out.println("testsendTextByOpenids:"+message);
          
         } catch (MalformedURLException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         } 
     }


4工具类


public class WxConstant {

 


public   String wxAppId ; // 微信AppId
public   String wxSecret = "xxx"; // 微信秘钥
public   String wxServer = "https://api.weixin.qq.com/"; // 微信服务器地址



//微信支付商户开通后 微信会提供appid和appsecret和商户号partner
public   String partners = "";
//这个参数partnerkey是在商户后台配置的一个32位的key,微信商户平台-账户设置-安全设置-api安全
public   String partnerkey = "";
//openId 是微信用户针对公众号的标识,授权的部分这里不解释
//微信支付成功后通知地址 必须要求80端口并且地址不能带参数  需修改
public   String notifyurl = "/xxx/weixin/getwxpayNotice.do";

public  String rechargenotifyurl = "/xxxx/weixin/getwxpayRechargeNotice.do";
public String getWxAppId() {
return wxAppId;
}


public void setWxAppId(String wxAppId) {
this.wxAppId = wxAppId;
}


public String getWxSecret() {
return wxSecret;
}


public void setWxSecret(String wxSecret) {
this.wxSecret = wxSecret;
}


public String getWxServer() {
return wxServer;
}


public void setWxServer(String wxServer) {
this.wxServer = wxServer;
}


 


public String getPartners() {
return partners;
}


public void setPartners(String partners) {
this.partners = partners;
}


public String getPartnerkey() {
return partnerkey;
}


public void setPartnerkey(String partnerkey) {
this.partnerkey = partnerkey;
}


public String getNotifyurl() {
return notifyurl;
}


public void setNotifyurl(String notifyurl) {
this.notifyurl = notifyurl;
}


public String getRechargenotifyurl() {
return rechargenotifyurl;
}


public void setRechargenotifyurl(String rechargenotifyurl) {
this.rechargenotifyurl = rechargenotifyurl;
}
 
}

你可能感兴趣的:(java微信发送图文和文字消息)