Java对接腾讯云短信和阿里云天气预报

API接口提供商

短信

这里推荐阿里云,比起其他的杂牌方便很多

因为现在审核环境的问题,个人开发者比较难申请到签名和模板

我们需要创建一个微信公众号来作为应用场景
在微信官方的公众号平台上面创建
在这里插入图片描述
公众号的步骤这里就省略了

在这里插入图片描述
这就是刚刚创建好的微信公众号
在阿里云控制台—短信服务
在这里插入图片描述
在这里插入图片描述
在国内消息里面申请签名和模板
在这里插入图片描述
但是在这里签名申请遇到了问题
我的签名申请连续两天被打回不通过
在这里插入图片描述
阿里云不支持个人公众号
在这里插入图片描述

于是转战腾讯云
在这里插入图片描述
一样控制台进去,短信,申请签名和模板
目前的腾讯云的签名申请对我这种学生或者个人开发者更加友好
在这里插入图片描述
成功
在这里插入图片描述
腾讯云的短信API接口的使用参考
java对接腾讯云短信,通过api发送短信
发送短信

天气预报

这里选择阿里云就好了
在云市场里面,购买一个免费的天气预报接口
在这里插入图片描述

java实现

短信

腾讯云的代码很简单
你需要知道的几个参数
1.模板ID
在这里插入图片描述
2.签名名字
在这里插入图片描述
3.手机号码
国内手机号码要+86
4.个人密钥
secretid和secretKey在腾讯云-API密钥管理中创建,secretid和secretKey具有非常高的访问权限,不能随意泄露给别人

之后就可以使用腾讯的在线调试平台调试了
调试

代码:

 try{

             Credential cred = new Credential("你的SecretId", "你的SecretKey");//个人密钥里面获取

             HttpProfile httpProfile = new HttpProfile();
             httpProfile.setEndpoint("sms.ap-chongqing.tencentcloudapi.com");

             ClientProfile clientProfile = new ClientProfile();
             clientProfile.setHttpProfile(httpProfile);

             SmsClient client = new SmsClient(cred, "", clientProfile);

             SendSmsRequest req = new SendSmsRequest();
             String[] phoneNumberSet1 = {"+86182xxxx846"};    //发送的手机,不要忘记国内+86
             req.setPhoneNumberSet(phoneNumberSet1);

             req.setTemplateID("829417");    //你的模板ID
             req.setSmsSdkAppid("1400467412");   //你的应用ID
             req.setSign("饿丸船业");      		//你的签名名字

             SendSmsResponse resp = client.SendSms(req);

             System.out.println(SendSmsResponse.toJsonString(resp));
         } catch (TencentCloudSDKException e) {
                 System.out.println(e.toString());
         }

     }

依赖:

<repository>
  <id>nexus-tencentyun</id>
  <name>Nexus tencentyun</name>
  <url>https://mirrors.tencent.com/nexus/repository/maven-public/</url>
</repository>

     <dependency>
            <groupId>com.github.qcloudsms</groupId>
            <artifactId>qcloudsms</artifactId>
            <version>1.0.6</version>
        </dependency>

		<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.87</version>
		</dependency>

效果:

在这里插入图片描述

天气预报

代码

package demo.ein;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.util.EntityUtils;

import com.aliyuncs.http.HttpResponse;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    	 String host = "https://jisutqybmf.market.alicloudapi.com";
 	    String path = "/weather/query";
 	    String method = "ANY";
 	    //GET/POST 任意
	    String appcode = "你购买的Appcode";
	    Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + appcode);
 	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("city", "重庆");
	    querys.put("citycode", "citycode");
	    querys.put("cityid", "cityid");
	    querys.put("ip", "ip");
	    querys.put("location", "location");


 	    try {

 	    	org.apache.http.HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
 	    	System.out.println(response.toString());
 	    	//获取response的body
 	    	String str=EntityUtils.toString(((org.apache.http.HttpResponse) response).getEntity(),"utf-8"); 
 	    	 String[] strarray=str.split(","); //遇到逗号就分割
 	    	 for (int i = 0; i < strarray.length; i++) 
 	    	 {
 	    		System.out.println(strarray[i]); 
 	    		if(strarray[i]=="{"||strarray[i]=="}")
 	    		{
 	    			System.out.println("\n"); //遇到{}就换行
 	    		}
 	    	 }

 	    } catch (Exception e) {
 	    	e.printStackTrace();
 	    }
    }
}

相关依赖:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.4.6</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-ecs</artifactId>
    <version>4.17.6</version>

运行结果如下,粗糙了点
在这里插入图片描述

遇到的问题

Eclipse 创建Maven项目Select an Archetype为空解决方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值