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

1、获取token

        public static Map<String, String> 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<String, String> map = new HashMap<String, String>();
  map.put("code", "001");
map.put("message", errcode + "," + errmsg);
return map;


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



2、创建需要发送的数据

  //创建发送的数据 文字消息
    private static String createSendDataToText(String openId,Map<String, String> 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<String, String> 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<String, String> map,WxConstant wxConstant){
        String urlstr =wxConstant.wxServer+"cgi-bin/message/custom/send?access_token=ACCESS_TOKEN"; //发送客服图文消息
        Map<String, String> 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;
}
 
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值