[实例]JAVA调用微信接口发送图文消息,不用跳到详情页

摘要:
=null){access_token=String.valueOf;}System.out.println;returnaccess_token;}catch{e.printStackTrace();returnaccess_token;}catch{e.printStackTrace();returnaccess_token;}}//获取该服务号下的用户组publicJSONArraygetOpenids(){JSONArrayarray=null;Stringurlstr="https://api.weixin.qq.com/cgi-bin/user/get?
packagecom.test;

importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;

importorg.junit.Test;

importnet.sf.json.JSONArray;
importnet.sf.json.JSONObject;

public classWechatServlet {
//获取微信返回的access_token
privateString getAccess_token() { String access_token=null; StringBuffer action =newStringBuffer(); action.append("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential") .append("&appid=***************") //设置服务号的appid .append("&secret=*********************************"); //设置服务号的密匙 URL url; try{
//模拟get请求 url
= newURL(action.toString()); HttpURLConnection http =(HttpURLConnection) url.openConnection(); http.setRequestMethod("GET"); http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoInput(true); InputStream is =http.getInputStream(); int size =is.available(); byte[] buf=new byte[size]; is.read(buf); String resp =new String(buf,"UTF-8"); JSONObject json =JSONObject.fromObject(resp); Object object =json.get("access_token"); if(object !=null){ access_token =String.valueOf(object); } System.out.println("getAccess_token:"+access_token); returnaccess_token; } catch(MalformedURLException e) { e.printStackTrace(); returnaccess_token; } catch(IOException e) { e.printStackTrace(); returnaccess_token; } } //获取该服务号下的用户组 publicJSONArray getOpenids(){ JSONArray array =null; String urlstr ="https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID"; urlstr =urlstr.replace("ACCESS_TOKEN", getAccess_token()); urlstr =urlstr.replace("NEXT_OPENID", ""); URL url; try{ url = newURL(urlstr); HttpURLConnection http =(HttpURLConnection) url.openConnection(); http.setRequestMethod("GET"); http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoInput(true); InputStream is =http.getInputStream(); int size =is.available(); byte[] buf=new byte[size]; is.read(buf); String resp =new String(buf,"UTF-8"); JSONObject jsonObject =JSONObject.fromObject(resp); System.out.println("getOpenids:"+jsonObject.toString()); array =jsonObject.getJSONObject("data").getJSONArray("openid"); returnarray; } catch(MalformedURLException e) { e.printStackTrace(); returnarray; } catch(IOException e) { e.printStackTrace(); returnarray; } } //根据用户组的openId获取用户详细数据 publicJSONObject getUserOpenids(String openId){ String urlstr ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN"; urlstr =urlstr.replace("ACCESS_TOKEN", getAccess_token()); urlstr =urlstr.replace("OPENID", openId); urlstr =urlstr.replace("NEXT_OPENID", ""); URL url; try{ url = newURL(urlstr); HttpURLConnection http =(HttpURLConnection) url.openConnection(); http.setRequestMethod("GET"); http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoInput(true); InputStream is =http.getInputStream(); int size =is.available(); byte[] buf=new byte[size]; is.read(buf); String resp =new String(buf,"UTF-8"); JSONObject jsonObject =JSONObject.fromObject(resp); System.out.println("getUserOpenids:"+jsonObject.toString()); returnjsonObject; } catch(MalformedURLException e) { e.printStackTrace(); return null; } catch(IOException e) { e.printStackTrace(); return null; } } //主方法,只有服务号才能使用客服图文消息,实现点击图文链接,直接跳转到指定URL @Test public voidtestsendTextByOpenids(){ //String urlstr ="https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN"; //群发图文消息 String urlstr ="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN"; //发送客服图文消息 urlstr =urlstr.replace("ACCESS_TOKEN", getAccess_token()); String reqjson =createGroupText(getOpenids()); try{ URL httpclient =newURL(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(); } } //创建发送的数据 privateString createGroupText(JSONArray array){ JSONObject gjson =newJSONObject(); //JSONObject json = getUserOpenids(array.get(3).toString()); //array参数是用户组所有的用户,该方法打印array其中一个用户的详细信息 gjson.put("touser", array.get(3)); gjson.put("msgtype", "news"); JSONObject news =newJSONObject(); JSONArray articles =newJSONArray(); JSONObject list =newJSONObject(); list.put("title","title"); //标题 list.put("description","description"); //描述 list.put("url","http://"); //点击图文链接跳转的地址 list.put("picurl","http://"); //图文链接的图片 articles.add(list); news.put("articles", articles); JSONObject text =newJSONObject(); gjson.put("text", text); gjson.put("news", news); returngjson.toString(); } }

免责声明:文章转载自《[实例]JAVA调用微信接口发送图文消息,不用跳到详情页》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇“windows无法访问指定设备路径或文件...”解决办法java通过jdbc连接数据库并更新数据(包括java.util.Date类型数据的更新)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Apache Beam实战指南 | 大数据管道(pipeline)设计及实践

Apache Beam实战指南 | 大数据管道(pipeline)设计及实践  mp.weixin.qq.com 策划 & 审校 | Natalie作者 | 张海涛编辑 | LindaAI 前线导读: 本文是 Apache Beam 实战指南系列文章第五篇内容,将对 Beam 框架中的 pipeline 管道进行剖析,并结合应用示例介绍如...

Solr与MySQL查询性能对比

测试环境 本文简单对比下Solr与MySQL的查询性能速度。 测试数据量:10407608     Num Docs: 10407608 普通查询 这里对MySQL的查询时间都包含了从MySQL Server获取数据的时间。 在项目中一个最常用的查询,查询某段时间内的数据,SQL查询获取数据,30s左右 SELECT * FROM `tf_hotspotd...

md5加密

package cn.easybuy.utils; import org.apache.commons.codec.digest.DigestUtils; public class SecurityUtils { /*** md5加密** @param value 要加密的值* @return md5加密后的值*/public static String...

SpringBoot整合Shiro 二:Shiro配置类

  环境搭建见上篇:SpringBoot整合Shiro 一:搭建环境   Shiro配置类配置 shiro的配置主要集中在 ShiroFilterFactoryBean 中       关于权限:   anon:无需认证就可以访问 ​  authc:必须认证了才能访问​   user:必须用有了 记住我 功能才能用​   perms:拥有对某个资源的...

asp.net简单实现利用HttpModule实现防sql注入

关于sql注入,已经被很多人讨论过了。这篇没有新意功能也不够通用,nnd,不想引起口水,就是觉得简单而且思路有参考性才贴出来。1、新建一个类,实现IHttpModule接口 代码     public class SqlHttpModule : IHttpModule    {        public void Dispose()        { ...

导入Excel——解析Excel——优化

package com.it.excel.excelLearn; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMa...