Android使用MobSDK短信验证
短信注册和短信验证已经是家常便饭了,所以当然要学习如何使用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 没有打开服务端验证开关
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: Java常用IO流