Android uses MOBSDK SMS to verify _android

Source: Internet
Author: User
Tags stringbuffer

SMS Registration and SMS verification is a commonplace, so of course to learn how to use the SDK
MOBSDK can send text messages for free, of course, use it.
http://www.mob.com

1. First Download SDK

2. Create a new folder called Lib in the project put 4 things in

3. Add the following code to the app Build.gradule

repositories{
  flatdir{
    dirs ' lib '//is where you put AAR directory address
  }

Dependencies {
  /
  /..... Other dependency pack
  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. Add permissions and activity to Androidmanifest.xml

<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 "/>

Add under 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. Start using

Initializing the SDK

SMSSDK.INITSDK (This, "Your Appkey", "Your Appsecret");

Send Verification Code

Smssdk.getverificationcode ("86", "Your mobile number");

6. Verify the verification code

Post to mob server to verify after receiving the verification code
First on the official network to open the service-side verification switch

The request address is:https://webapi.sms.mob.com/sms/verify

Request Method: POST

Request parameters
Appkey Application Appkey
Phone phone number
Zone Area code
code authentication code that needs to be validated

I have helped you to write the method of post, only the address and parameters can return the results

 /** * Initiate HTTPS request * @param address post addresses * @param params parameters * @return result/public static String Reque
    Stdata (string address, string params) {HttpURLConnection conn = null; try {//Create a trust manager this does not validate certificate chains trustmanager[] Trustallcerts = new T
        Rustmanager[]{new X509trustmanager () {public x509certificate[] Getacceptedissuers () {return null;} public void checkclienttrusted (x509certificate[] certs, String authtype) {} public void checkservertrusted (X509cert

      Ificate[] 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, SS
        Lsession 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;p ost 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;

 }

Since it's a network request, of course, new threads are used.

 New Thread (New Runnable () {
          @Override public
          void Run () {
            String params= "appkey= your appkey;phone= your cell phone number; zone =86;code= phone received the verification Code ";
            String result = RequestData ("https://webapi.sms.mob.com/sms/verify",
                params);
            Output
            System.out.println (result);
          }
        ). Start ();

Output result sample {status:200}
result Encoding

Return value Result description
200 Validation Success
405 Appkey is empty
406 Appkey Invalid
456 country code or cell phone number is empty
457 Phone number Format error
466 the verification code requesting validation is empty
467 request Check Verification code frequently (5 minutes the same appkey the same number can only be verified three times)
468 Authentication Code Error
474 does not open the service-side verification switch

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.