In my android application I have created one button, when I had pressed on the button I want to send message.So for that I have created one java class and written twilio cod
My method, using OkHttp:
1. Prerequisites
Gradle:
dependencies {
compile 'com.squareup.okhttp3:okhttp:3.4.1'
}
Manifest:
Permission in activity:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.setThreadPolicy( new StrictMode.ThreadPolicy.Builder().permitAll().build() );
}
2. Code
private void sendSms(String toPhoneNumber, String message){
OkHttpClient client = new OkHttpClient();
String url = "https://api.twilio.com/2010-04-01/Accounts/"+ACCOUNT_SID+"/SMS/Messages";
String base64EncodedCredentials = "Basic " + Base64.encodeToString((ACCOUNT_SID + ":" + AUTH_TOKEN).getBytes(), Base64.NO_WRAP);
RequestBody body = new FormBody.Builder()
.add("From", fromPhoneNumber)
.add("To", toPhoneNumber)
.add("Body", message)
.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.header("Authorization", base64EncodedCredentials)
.build();
try {
Response response = client.newCall(request).execute();
Log.d(TAG, "sendSms: "+ response.body().string());
} catch (IOException e) { e.printStackTrace(); }
}
I used Allu code for generathing authorization in header