Android™ Requires Chilkat v11.0.0+
Android™
Check an SMS verification code with Twilio Verify
See more Twilio Examples
After the user enters the SMS code, retrieve the VE verification SID saved when you started the verification. Send that SID and the entered code to the same Verify Service. Keep the code as text so a leading zero survives.
Mark the phone number on that pending attempt as verified only when Twilio returns status=approved. A pending check leaves the number unverified. Twilio can return HTTP 404 after expiration, approval, or exhaustion of check attempts. Keep a successful result in your application so it can be used after Twilio removes the verification.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Obtain these credentials from the application's private configuration.
CkHttp http = new CkHttp();
http.put_RequireSslCertVerify(true);
http.put_BasicAuth(true);
http.put_Login("YOUR_API_KEY_SID");
http.put_Password("YOUR_API_KEY_SECRET");
// Both requests use the same Verify Service SID (VA...).
CkStringBuilder url = new CkStringBuilder();
url.Append("https://verify.twilio.com/v2/Services/");
url.Append("YOUR_VERIFY_SERVICE_SID");
url.Append("/VerificationCheck");
CkHttpRequest req = new CkHttpRequest();
req.put_HttpVerb("POST");
req.put_ContentType("application/x-www-form-urlencoded");
// Load the saved SID for this user's pending attempt. Take only the code from user input.
String verificationSid = "SAVED_VERIFICATION_SID";
String enteredCode = "CODE_ENTERED_BY_USER";
req.AddParam("VerificationSid",verificationSid);
req.AddParam("Code",enteredCode);
CkHttpResponse resp = new CkHttpResponse();
success = http.HttpReq(url.getAsString(),req,resp);
if (resp.get_StatusCode() == 0) {
Log.i(TAG, "No response from Twilio. Check connectivity and TLS settings.");
return;
}
if (resp.get_StatusCode() == 404) {
Log.i(TAG, "No active verification found. Check the service and verification SIDs.");
return;
}
CkJsonObject result = new CkJsonObject();
boolean parsed = result.Load(resp.bodyStr());
if ((success != true) or (resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300)) {
Log.i(TAG, "Twilio returned HTTP " + String.valueOf(resp.get_StatusCode()));
if ((parsed == true) and (result.JsonTypeOf("code") == 2)) {
Log.i(TAG, "Twilio error code: " + String.valueOf(result.IntOf("code")));
}
return;
}
if ((parsed != true) or (result.JsonTypeOf("status") != 1)) {
Log.i(TAG, "Verification status missing from the response.");
return;
}
String status = result.stringOf("status");
if (status == "approved") {
// Record success for the user and phone number associated with verificationSid.
Log.i(TAG, "Phone number verified.");
}
else {
if (status == "pending") {
Log.i(TAG, "Code not accepted. Check the digits and enter the code again.");
}
else {
Log.i(TAG, "Phone number unverified. Verification status: " + status);
}
}
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}