使用OkHttp和Retrofit发送网易云信验证码

短信服务(Short Message Service)是网易网易云通信为用户提供的一种通信服务的能力,目前支持验证码类短信、通知类短信、运营类短信、语音类短信、国际短信等事务性短信。网易网易云通信短信功能具体有全网覆盖、3-5 秒可达、超高到达率、7*24 小时服务监控等优势。按量付费、阶梯定价,发送越多单价越低。API调用简单,加快接入速度。

我们这里主要介绍使用OkHttp和Retrofit来做一些请求,就不做介绍了,直接使用代码来注释。

OkHttp

private final static String vercodeserverurl = "https://api.netease.im/sms/sendcode.action";
    private final static String vercodeappkey = "5970a1e************46ae";
    private final static String vercodeappsecret = "5*****4";
    private final static String vercodetemplateid = "30******7";

    public static void main(String[] args) throws IOException {
        String nonce = ((int) (Math.random() * 100000)) + "";
        String curTime = String.valueOf(System.currentTimeMillis() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(vercodeappsecret, nonce, curTime);

        FormBody.Builder builder = new FormBody.Builder();
        builder.add("mobile", "155********");
        RequestBody formBody = builder.build();

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(vercodeserverurl)
                .addHeader("AppKey",vercodeappkey)
                .addHeader("Nonce",nonce)
                .addHeader("CurTime",curTime)
                .addHeader("CheckSum",checkSum)
                .post(formBody)
                .build();
        okhttp3.Response execute = client.newCall(request).execute();
        ResponseBody body = execute.body();
        System.out.println(body.string());
        System.out.println("完成");

Retrofit

接口声明

public interface WebInterface {

    @FormUrlEncoded
    @POST("sms/sendcode.action")
    Call<MessageResponse> sendMessage(@Header("AppKey") String  apiKey,
                                      @Header("Nonce") String  Nonce,
                                      @Header("CurTime") String  CurTime,
                                      @Header("CheckSum") String  CheckSum,
                                      @Field("mobile") String mobile);
}

返回实体封装

public class MessageResponse {


    private int code;
    private String msg;
    private String obj;

	//省略全参构造

    @Override
    public String toString() {
        return "MessageResponse{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", obj='" + obj + '\'' +
                '}';
    }
}

Retrofit调用接口

public class SendMessage {

    private final static String vercodeserverurl="https://api.netease.im/";
    private final static String vercodeappkey="5970a1*************46ae";
    private final static String vercodeappsecret="5***********4";
    private final static String vercodetemplateid="3******7";
    private final static String content="application/x-www-form-urlencoded;charset=utf-8";

    public static void main(String[] args) throws IOException {
        String nonce = ((int) Math.random() * 100000) + "";
        String curTime = String.valueOf(System.currentTimeMillis() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(vercodeappsecret, nonce, curTime);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(vercodeserverurl) //设置网络请求的Url地址
                .addConverterFactory(GsonConverterFactory.create()) //设置数据解析器
                .build();


        WebInterface webInterface = retrofit.create(WebInterface.class);
        Call<MessageResponse> messageResponseCall = webInterface.sendMessage(vercodeappkey,
                nonce,
                curTime,
                checkSum,
                //content,
                "155*********");
				//同步执行
        Response<MessageResponse> execute = messageResponseCall.execute();
        MessageResponse messageResponse= execute.body();
		//判断是否成功等代码省略
    }
}
posted @ 2019-02-15 10:21  燕归来兮  阅读(397)  评论(0编辑  收藏  举报