Android™ Requires Chilkat v11.0.0+
Android™
Validate and format a phone number with Twilio Lookup
See more Twilio Examples
Use Basic Lookup v2 to check a national-format number and obtain its E.164 and national display formats. Supply the two-letter country code for a national number. The example requests only the basic fields.
Read the boolean valid field even when the HTTP request succeeds. When it is false, display any validation_errors that Twilio supplies. Numbering-plan validation does not establish reachability or control of a phone. To confirm control, send an SMS verification code.
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.
// Load an API key for the Twilio account from private application 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");
// The phone number is one URL path segment. Percent-encode spaces and any leading +.
CkStringBuilder number = new CkStringBuilder();
number.Append("020 7946 0018");
boolean encoded = number.Encode("url_rfc3986","utf-8");
if (encoded != true) {
Log.i(TAG, "Could not encode the phone number.");
return;
}
CkStringBuilder url = new CkStringBuilder();
url.Append("https://lookups.twilio.com/v2/PhoneNumbers/");
url.Append(number.getAsString());
url.Append("?CountryCode=GB");
CkHttpResponse resp = new CkHttpResponse();
CkJsonObject reply = new CkJsonObject();
success = http.HttpNoBody("GET",url.getAsString(),resp);
if (resp.get_StatusCode() == 0) {
Log.i(TAG, "No HTTP response from Twilio.");
return;
}
boolean parsed = reply.Load(resp.bodyStr());
if ((success != true) or (resp.get_StatusCode() != 200)) {
Log.i(TAG, "Twilio returned HTTP " + String.valueOf(resp.get_StatusCode()));
if ((parsed == true) and (reply.JsonTypeOf("code") == 2)) {
Log.i(TAG, "Twilio error: " + String.valueOf(reply.IntOf("code")) + " " + reply.stringOf("message"));
}
return;
}
if (parsed != true) {
Log.i(TAG, "Expected a JSON response from Twilio.");
return;
}
if (reply.JsonTypeOf("valid") != 5) {
Log.i(TAG, "The lookup response is missing the validity flag.");
return;
}
if (reply.BoolOf("valid") != true) {
Log.i(TAG, "The number is invalid for the specified country.");
int i = 0;
while ((i < reply.SizeOfArray("validation_errors"))) {
reply.put_I(i);
Log.i(TAG, reply.stringOf("validation_errors[i]"));
i = i + 1;
}
return;
}
if (reply.JsonTypeOf("phone_number") != 1) {
Log.i(TAG, "The lookup response is missing the E.164 number.");
return;
}
Log.i(TAG, "E.164: " + reply.stringOf("phone_number"));
Log.i(TAG, "National format: " + reply.stringOf("national_format"));
}
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."
}
}