短信微信推送

1、循环发短信微信
Sendinfo s=new Sendinfo();
String tokeTest =s.getAccess_token("wx0657d7c14620581c","cafbeb95c7a4709f4e128d01b314fa3b") ;
JSONObject jsonObject1 = JSONObject.fromObject(tokeTest.toString());
String groupUrl1 = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+jsonObject1.get("access_token");
//String OpenId =a.get(0).getWxOpenId();
List<User> userlist = announceService.findidphone(user);
for (int i = 0; i < userlist.size(); i++) {
User us = userlist.get(i);
String OpenId = us.getWxOpenId();
/*String phone = us.getPhone();
String name = us.getUserName();*/
String str = "您好!您有新的公告!"+'\n'+'\t'+"公告标题:"+announce.getTitle()+'\n'+'\t'+'\n'+'\t'+"发送人部门:"+announce.getSecName()+'\n'+'\t'+"发送人姓名:"+announce.getUserName();
String openid1data1 = "{" + "\"touser\": \"" + OpenId + "\","
+ "\"msgtype\": \"text\"," + "\"text\": {"
+ "\"content\": \" " + str + " \"" + "}" + "}";
JSONObject jsonObject =s.httpsRequest1(groupUrl1, "post",
openid1data1);
/*String smsM = "SMS_117517755";
String count = "{\"name\":\""+name+"\"}" ;
SendSmsResponse sp = sendSms(phone,smsM,count);*/

2、Sendinfo s=new Sendinfo();
String tokeTest =s.getAccess_token("wx0657d7c14620581c","cafbeb95c7a4709f4e128d01b314fa3b") ;
JSONObject jsonObject1 = JSONObject.fromObject(tokeTest.toString());
String groupUrl1 = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+jsonObject1.get("access_token");
String OpenId =a.get(0).getWxOpenId();
String str = "您好!您有新的任务!"+'\n'+'\t'+"任务名称:"+task.getTask_Name()+'\n'+'\t'+"紧急程度:"+mm+'\n'+'\t'+"发送人部门:"+a.get(0).getSecName()+'\n'+'\t'+"发送人姓名:"+a.get(0).getUserName();
String openid1data1 = "{" + "\"touser\": \"" + OpenId + "\","
+ "\"msgtype\": \"text\"," + "\"text\": {"
+ "\"content\": \" " + str + " \"" + "}" + "}";
JSONObject jsonObject =s.httpsRequest1(groupUrl1, "post",
openid1data1);
//想手机发送短信
String phone = a.get(0).getPhone();
String smsM = "SMS_117517755";
String name = a.get(0).getUserName();
String count = "{\"name\":\""+name+"\"}" ;
SendSmsResponse sp = sendSms(phone,smsM,count);
Thread.sleep(1000L);
3、两个jar包
aliyun-java-sdk-core-3.3.1.jar
aliyun-java-sdk-dysmsapi-1.0.0.jar

4、封装一个类
package zygh.yfb.controller;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.json.JSONObject;

public class Sendinfo {
public String getAccess_token(String appId,String appsecret) {

String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="

+ appId+ "&secret=" + appsecret;

String accessToken = null;

try {

URL urlGet = new URL(url);

HttpURLConnection http = (HttpURLConnection) urlGet

.openConnection();

http.setRequestMethod("GET"); // 必须是get方式请求

http.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

http.setDoOutput(true);

http.setDoInput(true);

System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒

System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒

http.connect();

InputStream is = http.getInputStream();

int size = is.available();

byte[] jsonBytes = new byte[size];

is.read(jsonBytes);

String message = new String(jsonBytes, "UTF-8");

/* JSONObject demoJson = new JSONObject(message);

accessToken = demoJson.getString("access_token");*/

System.out.println(message);
accessToken = message ;

is.close();

} catch (Exception e) {

e.printStackTrace();

}

return accessToken;
}

/**
* 发送https请求
*
* @param requestUrl
* 请求地址
* @param requestMethod
* 请求方式(GET、POST)
* @param outputStr
* 提交的数据
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
*/
public JSONObject httpsRequest1(String requestUrl, String requestMethod,
String outputStr) {
JSONObject jsonObject = null;
String add_url = requestUrl;
String query = outputStr;
try {
URL url = new URL(add_url);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());

// System.out.println("query: "+query);
out.write(query.getBytes("UTF-8"));
out.flush();
out.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sbf = new StringBuffer();
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sbf.append(lines);
//System.out.println("返回:"+lines);
}
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值