Android™ Requires Chilkat v11.0.0+
Android™
Send an SMS with Twilio
See more Twilio Examples
Submit an SMS with the sender, recipient, and text as form fields. This example starts with Twilio test credentials and its successful test sender. Twilio simulates the submission without sending or billing for a message. Test credentials and test numbers.
For live SMS, use the live Account SID in the URL, authenticate with an API key SID and secret, and use an SMS-capable Twilio sender and a recipient in E.164 format. HTTP 201 creates the message. Save its SID to check delivery status later.
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.
CkHttp http = new CkHttp();
http.put_RequireSslCertVerify(true);
http.put_BasicAuth(true);
// Use the test Account SID and test Auth Token from the Twilio Console.
String accountSid = "YOUR_TEST_ACCOUNT_SID";
http.put_Login(accountSid);
http.put_Password("YOUR_TEST_AUTH_TOKEN");
CkStringBuilder url = new CkStringBuilder();
url.Append("https://api.twilio.com/2010-04-01/Accounts/");
url.Append(accountSid);
url.Append("/Messages.json");
// Add raw field values. Chilkat form-encodes them, including the + in a phone number.
CkHttpRequest req = new CkHttpRequest();
req.put_HttpVerb("POST");
req.put_ContentType("application/x-www-form-urlencoded");
req.AddParam("From","+15005550006");
req.AddParam("To","+12025550123");
req.AddParam("Body","Hello from Twilio!");
CkHttpResponse resp = new CkHttpResponse();
CkJsonObject reply = new CkJsonObject();
success = http.HttpReq(url.getAsString(),req,resp);
if (resp.get_StatusCode() == 0) {
Log.i(TAG, http.lastErrorText());
return;
}
boolean parsed = reply.Load(resp.bodyStr());
if ((success != true) or (resp.get_StatusCode() != 201)) {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()));
if ((parsed == true) and (reply.HasMember("code") == true)) {
Log.i(TAG, "Twilio error " + String.valueOf(reply.IntOf("code")) + ": " + reply.stringOf("message"));
}
return;
}
if (parsed != true) {
Log.i(TAG, "Expected a JSON response.");
return;
}
if ((reply.JsonTypeOf("sid") != 1) or (reply.JsonTypeOf("status") != 1)) {
Log.i(TAG, "The create response is missing the message SID or status.");
return;
}
Log.i(TAG, "Message SID: " + reply.stringOf("sid"));
Log.i(TAG, "Initial status: " + reply.stringOf("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."
}
}