首页 > 系统 > Android > 正文

Android使用MobSDK短信验证

2019-12-12 05:45:15
字体:
来源:转载
供稿:网友

短信注册和短信验证已经是家常便饭了,所以当然要学习如何使用SDK啦
MobSDK可以免费发短信,当然就用它啦
http://www.mob.com

1.首先下载sdk

2.在项目新建一个叫lib的文件夹把4个东西放进去

3.在app的build.gradule添加如下代码

repositories{  flatDir{    dirs 'lib' //就是你放aar的目录地址  }}dependencies {  //.......  //其他依赖包  compile name: 'SMSSDK-2.1.0', ext: 'aar'  compile name: 'SMSSDKGUI-2.1.0', ext: 'aar'  compile files('lib/MobCommons-2016.0624.1326.jar')  compile files('lib/MobTools-2016.0624.1326.jar')}

4.在AndroidManifest.xml中添加权限和activity

<uses-permission android:name="android.permission.READ_CONTACTS" />  <uses-permission android:name="android.permission.READ_PHONE_STATE" />  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  <uses-permission android:name="android.permission.INTERNET" />  <uses-permission android:name="android.permission.RECEIVE_SMS" />  <uses-permission android:name="android.permission.READ_SMS" />  <uses-permission android:name="android.permission.GET_TASKS" />  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

在application下添加

<activity      android:name="com.mob.tools.MobUIShell"      android:theme="@android:style/Theme.Translucent.NoTitleBar"      android:configChanges="keyboardHidden|orientation|screenSize"      android:windowSoftInputMode="stateHidden|adjustResize"/>

5.开始使用

初始化SDK

SMSSDK.initSDK(this, "您的appkey", "您的appsecret");

发送验证码

SMSSDK.getVerificationCode("86","您的手机号");

6.验证验证码

收到验证码后要post到Mob的服务器上验证
首先在到官网上开通服务端验证开关

请求地址为:https://webapi.sms.mob.com/sms/verify

请求方式: POST

请求参数
 •appkey 应用appkey
 •phone 电话号码
 •zone 区号
 •code 需要验证的验证码

我已经帮你写好post的方法内容,只需传地址和参数就能返回结果

 /**   * 发起https请求   * @param address  post地址   * @param params  参数   * @return 结果   */  public static String requestData(String address ,String params){    HttpURLConnection conn = null;    try {      // Create a trust manager that does not validate certificate chains      TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){        public X509Certificate[] getAcceptedIssuers(){return null;}        public void checkClientTrusted(X509Certificate[] certs, String authType){}        public void checkServerTrusted(X509Certificate[] certs, String authType){}      }};      // Install the all-trusting trust manager      SSLContext sc = SSLContext.getInstance("TLS");      sc.init(null, trustAllCerts, new SecureRandom());      //ip host verify      HostnameVerifier hv = new HostnameVerifier() {        public boolean verify(String urlHostName, SSLSession session) {          return urlHostName.equals(session.getPeerHost());        }      };      //set ip host verify      HttpsURLConnection.setDefaultHostnameVerifier(hv);      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());      URL url = new URL(address);      conn = (HttpURLConnection) url.openConnection();      conn.setRequestMethod("POST");// POST      conn.setConnectTimeout(3000);      conn.setReadTimeout(3000);      // set params ;post params      if (params!=null) {        conn.setDoOutput(true);        DataOutputStream out = new DataOutputStream(conn.getOutputStream());        out.write(params.getBytes(Charset.forName("UTF-8")));        out.flush();        out.close();      }      conn.connect();      //get result      if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {        InputStream in=conn.getInputStream();        StringBuffer  out  =  new  StringBuffer();        byte[]  b  =  new  byte[4096];        for  (int  n;  (n  =  in.read(b))  !=  -1;)  {          out.append(new  String(b,  0,  n));        }        return out.toString();      } else {        System.out.println(conn.getResponseCode() + " "+ conn.getResponseMessage());      }    } catch (Exception e) {      e.printStackTrace();    } finally {      if (conn != null)        conn.disconnect();    }    return null;  }

既然是网络请求当然要新建线程使用啦

 new Thread(new Runnable() {          @Override          public void run() {            String params="appkey=您的appkey;phone=您的手机号;zone=86;code=手机收到的验证码";            String result = requestData("https://webapi.sms.mob.com/sms/verify",                params);            //输出结果            System.out.println(result);          }        }).start();

输出结果样例{status:200}
 结果编码

返回值 结果描述
200 验证成功
405 AppKey为空
406 AppKey无效
456 国家代码或手机号码为空
457 手机号码格式错误
466 请求校验的验证码为空
467 请求校验验证码频繁(5分钟内同一个appkey的同一个号码最多只能校验三次)
468 验证码错误
474 没有打开服务端验证开关

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表