首页 > 代码库 > 【转+修改】容联云通讯api调用短信发送调用

【转+修改】容联云通讯api调用短信发送调用

转自   https://my.oschina.net/u/1995134/blog/814540

需要荣联云通讯 的 相对应SDKjar包。

CCP_REST_SMS_SDK_JAVA_v2.6.3r.jar

调用测试在main方法里:

  public static void main(String[] args){
        ResponseObj obj =   FhPhoneMassageSender.testSender("176****5720","呵呵"); -- 手机号码及内容
        System.out.println(obj.getResultMsg());
    }

上代码:

import java.util.HashMap;
import java.util.Set;

import com.cloopen.rest.sdk.CCPRestSmsSDK;public class FhPhoneMassageSender {
    private static String  sms_ytx_url="appsms.cloopen.com";
    private static String  sms_ytx_port="8883";
    private static String  sms_ytx_sid="*****************ff2dea1de4a6f";
    private static String  sms_ytx_token="fa9b5d*************2457508977";
    private static String  sms_ytx_appid="aaf98f89544cd9d9015475b429082343";
    private static String  sms_ytx_valid_min="5";
    private static String  sms_ytx_tempid="8****40";
    /**
     * <p class="detail">
     * 功能:容联云通讯-发送短信
     * </p>
     * @author liuwh
     * @date 2016-1-22
     * @param tel    手机号码,多个用,分隔
     * @param verifyCode  验证码
     * @return
     */
    public static ResponseObj sendRegCodeByYTX(String tel, String verifyCode,String verifyName){

        ResponseObj obj = new ResponseObj(true, "操作成功");
        HashMap<String, Object> result = null;
        //初始化SDK
        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();

        restAPI.init(Global.getConfig("sms_ytx_url"), Global.getConfig("sms_ytx_port"));

        restAPI.setAccount(Global.getConfig("sms_ytx_sid"), Global.getConfig("sms_ytx_token"));//账号、密码

        restAPI.setAppId(Global.getConfig("sms_ytx_appid"));//应用ID

        String smsValidMin=Global.getConfig("sms_ytx_valid_min");    //验证码过期时间
        if(StringUtils.isEmpty(smsValidMin)){
            smsValidMin="5";
        }
        result = restAPI.sendTemplateSMS(tel, Global.getConfig("sms_ytx_tempid"),new String[]{verifyName,verifyCode, smsValidMin});

        System.out.println("SDKTestGetSubAccounts result=" + result);
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息(map)
            HashMap<String,Object> data = http://www.mamicode.com/(HashMap) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
            obj.setSuccessful(false);
            obj.setResultMsg(String.valueOf(result.get("statusMsg")));
        }
        return obj;
    }

    /**
     * <p class="detail">
     * 功能:容联云通讯-发送催款信息
     * </p>
     * @author liuwh
     * @date 2016-1-22
     * @param tel    手机号码,多个用,分隔
     * @param verifyName 参数值(多个替换坑)
     * @return
     */
    public static ResponseObj sendPressMoneyByYTX(String tel,String... verifyName){

        ResponseObj obj = new ResponseObj(true, "操作成功");
        HashMap<String, Object> result = null;
        //初始化SDK
        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();

        restAPI.init(sms_ytx_url, sms_ytx_port);

        restAPI.setAccount(Global.getConfig("sms_ytx_sid"), Global.getConfig("sms_ytx_token"));//账号、密码

        restAPI.setAppId(Global.getConfig("sms_ytx_appid"));//应用ID

        result = restAPI.sendTemplateSMS(tel, Global.getConfig("sms_ytx_press_tempid"), verifyName);

        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息(map)
            HashMap<String,Object> data = http://www.mamicode.com/(HashMap) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
            obj.setSuccessful(false);
            obj.setResultMsg(String.valueOf(result.get("statusMsg")));
        }
        return obj;
    }
    public static ResponseObj testSender(String tel,String... verifyName){
        ResponseObj obj = new ResponseObj(true, "操作成功");
        HashMap<String, Object> result = null;
        //初始化SDK
        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();

        restAPI.init(sms_ytx_url,sms_ytx_port);

        restAPI.setAccount(sms_ytx_sid, sms_ytx_token);//账号、密码

        restAPI.setAppId(sms_ytx_appid);//应用ID

        result = restAPI.sendTemplateSMS(tel, sms_ytx_tempid, verifyName);

        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息(map)
            HashMap<String,Object> data = http://www.mamicode.com/(HashMap) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
            obj.setSuccessful(false);
            obj.setResultMsg(String.valueOf(result.get("statusMsg")));
        }
        return obj;
    }
    public static void main(String[] args){
        ResponseObj obj =   FhPhoneMassageSender.testSender("176****5720","呵呵");
        System.out.println(obj.getResultMsg());
    }
}
Global 是用来获取配置文件内容的。实际开发中可以像测试代码中那样把api需要的配置信息直接写在静态变量中。
ResponseObj
是自己封装的返回数据的类型:
package com.thinkgem.jeesite.common.utils;

/**
 * Created by antis on 2017/6/27.
 */
public class ResponseObj {
    /**
     * 总记录数
     */
    private int total;
    /**
     * 当前记录集合
     */
    private Object data;
    /**
     * 是否成功
     */
    private boolean successful;
    /**
     * 结果消息
     */
    private String resultMsg;
    /**
     * 错误类型
     */
    private String type;
    /**
     * 添加数据
     * @param data
     * @return
     */
    public static ResponseObj successResult(Object data) {
        return new ResponseObj(1,data,true, "", "");
    }
    /**
     * 添加数据
     * @param data
     * @return
     */
    public static ResponseObj successResult(Object data,int total) {
        return new ResponseObj(total,data,true, "", "");
    }
    /**
     * 返回失败信息
     * @param exMessage
     * @return
     */
    public static ResponseObj failedResult(String exMessage) {
        return new ResponseObj(0, "", false,exMessage, "error");
    }
    /**
     * 返回失败信息和类型
     * @param exMessage
     * @param type
     * @return
     */
    public static ResponseObj failedResult(String exMessage, String type) {
        return new ResponseObj(0, "",false, exMessage, type);
    }
    /**
     * 构造函数,类型为error
     *
     */
    public ResponseObj() {
        type = "error";
    }
    public ResponseObj(boolean successful,String resultMsg){
        this.successful = successful;
        this.resultMsg = resultMsg;
    }
    public ResponseObj(int total, Object data, boolean successful,String resultMsg, String type) {
        this.total = total;
        this.data =http://www.mamicode.com/ data;
        this.successful = successful;
        this.resultMsg = resultMsg;
        this.type = type;
    }
    /**
     * @return the 总记录数
     */
    public int getTotal() {
        return total;
    }
    /**
     * @param total 总记录数 the total to set
     */
    public void setTotal(int total) {
        this.total = total;
    }
    /**
     * @return the 是否成功
     */
    public boolean isSuccessful() {
        return successful;
    }

    /**
     * @param successful 是否成功
     *            the successful to set
     */
    public void setSuccessful(boolean successful) {
        this.successful = successful;
    }


    /**
     * @return the data
     */
    public Object getData() {
        return data;
    }
    /**
     * @param data the data to set
     */
    public void setData(Object data) {
        this.data =http://www.mamicode.com/ data;
    }
    /**
     * @return the 结果消息
     */
    public String getResultMsg() {
        return resultMsg;
    }

    /**
     * @param resultMsg 结果消息
     *            the resultMsg to set
     */
    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    /**
     * @return the 错误类型
     */
    public String getType() {
        return type;
    }

    /**
     * @param type 错误类型
     *            the type to set
     */
    public void setType(String type) {
        this.type = type;
    }
}
  解释一下api需要的各个参数
   private static String  sms_ytx_url="appsms.cloopen.com"; --容联的appsms的url是固定不变的
    private static String  sms_ytx_port="8883";              --端口号,应该也是不变的
    private static String  sms_ytx_sid="*****************ff2dea1de4a6f";  --容联主账户的ACCOUNT SID
    private static String  sms_ytx_token="fa9b5d*************2457508977"; --容联主账户的AUTH TOKEN
    private static String  sms_ytx_appid="aaf98f89544cd9d9015475b429082343";  --正在开发的应用的 APP ID
    private static String  sms_ytx_valid_min="5"; --验证码有效时间 可以自己设置,分钟为单位
    private static String  sms_ytx_tempid="8****40"; --短信模板ID

技术分享

 

 技术分享

技术分享

 

【转+修改】容联云通讯api调用短信发送调用