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

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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;publicclassWechatServlet {

//获取微信返回的access_tokenPRivateString 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();intsize =is.available();byte[] buf=newbyte[size];

is.read(buf);

String resp=newString(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();intsize =is.available();byte[] buf=newbyte[size];

is.read(buf);

String resp=newString(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();intsize =is.available();byte[] buf=newbyte[size];

is.read(buf);

String resp=newString(buf,"UTF-8");

JSONObject jsonObject=JSONObject.fromObject(resp);

System.out.println("getUserOpenids:"+jsonObject.toString());returnjsonObject;

}catch(MalformedURLException e) {

e.printStackTrace();returnnull;

}catch(IOException e) {

e.printStackTrace();returnnull;

}

}

//主方法,只有服务号才能使用客服图文消息,实现点击图文链接,直接跳转到指定URL

@TestpublicvoidtestsendTextByOpenids(){//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();intsize =is.available();byte[] jsonBytes =newbyte[size];

is.read(jsonBytes);

String message=newString(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();

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航