编程语言
首页 > 编程语言> > Java实现腾讯云短信定时推送天气信息(通俗易懂)

Java实现腾讯云短信定时推送天气信息(通俗易懂)

作者:互联网

准备:

代码:

		//腾讯云jar包
		<dependency>
			<groupId>com.tencentcloudapi</groupId>
			<artifactId>tencentcloud-sdk-java</artifactId>
			<!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
			<!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
			<version>3.1.322</version>
		</dependency>
		//httpclient包
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.13</version>
		</dependency>
		// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
		//自己的secretId,secretKey
        Credential cred = new Credential(secretId,secretKey);

        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        SendSmsRequest req = new SendSmsRequest();
        //要发送的手机号,数组形式可以发送多个
        String[] phoneNumberSet1 = {"+86xxxxxxxx","+86xxxxxx"};
        req.setPhoneNumberSet(phoneNumberSet1);
		//自己的sdkAppId
        req.setSmsSdkAppId("smsSdkAppid");
        //自己申请签名的签名内容,用哪一个短信开头的[]里的内容就是哪个
        //类似于[xx科技]xxxxxx
        req.setSignName("xx");
        //模板id
        req.setTemplateId("xxx");
		//json转天气对象
        JSONObject jsonObject = JSONObject.parseObject(wether()).getJSONObject("data");
        JSONArray forecast = jsonObject.getJSONArray("forecast");
        JSONObject o = forecast.getJSONObject(0);
   		//向短信模板传递参数,依次对应模板内容的{1},{2},{3}...
     	 String[] templateParamSet1 = {o.getString("date"),o.getString("type"), o.getString("high"),o.getString("low"),o.getString("fengxiang")};
     	 //天气格式
         //"{\"date\":\"26日星期五\",\"high\":\"高温 16℃\",\"fengli\":\"<![CDATA[2级]]>\",\"low\":\"低温 10℃\",\"fengxiang\":\"北风\",\"type\":\"晴\"}"
         req.setTemplateParamSet(templateParamSet1);
        // 返回的resp是一个SendSmsResponse的实例,与请求对象对应
        SendSmsResponse resp = client.SendSms(req);
        // 输出json格式的字符串回包
        System.out.println(SendSmsResponse.toJsonString(resp));

天气:

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Get请求
        //在city后边拼接想要获得天气的城市名就可以
        //http://wthrcdn.etouch.cn/weather_mini?city=上海
        HttpGet httpGet = new HttpGet("http://wthrcdn.etouch.cn/weather_mini?city=");
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
               
                return EntityUtils.toString(responseEntity,"utf-8");
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
	//每天早上7.30发
	
	@Scheduled(cron = "00 30 07 * * *")
    public void send() throws TencentCloudSDKException {
        logger.info("时间:{} 发送早上短信请求",simpleDateFormat.format(System.currentTimeMillis()));
        sendSms();
    }
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));

标签:null,Java,getString,req,通俗易懂,new,推送,response,模板
来源: https://blog.csdn.net/m0_48358308/article/details/121579958