techtinkerer
techtinkerer

Reputation: 1300

Error Sending SMS with Twilio on android wih TwilioRestClient

I want to send SMS from my android app. I have the SID and AUTHTOKEN pasted below. Appreciate someone looking and giving some input or working code:

Issue : there seem to be multiple ways to do it - use twilio-java-sdk, twilioclient-android and using REST API.

Here is the code I tried with TwilioRestClient which throws error as :

public static void SendSMS() throws TwilioRestException {
    TwilioRestClient client = new TwilioRestClient(Utils.TWILO_SID, Utils.TWILO_AUTH_TOKEN);

    Map<String,String> map = new HashMap<>();
    map.put("from", "4254434895");
    map.put("to", "4254434294");
    map.put("Body", "test message");

    SmsFactory factory = client.getAccount().getSmsFactory();
    Sms sms = factory.create(map);

    Log.d("TwilioHelper", sms.getSid());
    Log.d("TwilioHelper", sms.toString());
}

Error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.cyoo.app, PID: 3088
    java.lang.NoSuchMethodError: No virtual method setDefaultMaxPerRoute(I)V in class Lorg/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager; or its super classes (declaration of 'org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager' appears in /system/framework/ext.jar)
        at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:139)
        at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:109)
        at com.cyoo.app.TwilioHelper.SendSMS(TwilioHelper.java:16)
        at com.cyoo.app.RegiserFinalActivity$2.onClick(RegiserFinalActivity.java:71)
        at android.view.View.performClick(View.java:4785)

I will appreciate anyone throwing some sample working code. -Thanks

Upvotes: 0

Views: 488

Answers (2)

techtinkerer
techtinkerer

Reputation: 1300

I got it to work the following way, just using REST API.Posting as it could be useful for someone.

Yes, I agree with Philnash that there is problem with someone decompiling our code. So, definitely save and get credentials from server.

  public static void SendSMS(String targetPhoneNumber) {
    String postURL = "https://api.twilio.com/2010-04-01/Accounts/"+Utils.TWILO_SID+"/Messages";

    try {
        String base64EncodedCredentials = "Basic "
                + Base64.encodeToString(
                (Utils.TWILO_SID + ":" + Utils.TWILO_AUTH_TOKEN).getBytes(),
                Base64.NO_WRAP);

        RequestBody formBody = new FormBody.Builder()
                .add("From", "+1xxxxxx")  // number we get from Twilio
                .add("To", "+1xxxxxxxxx") //TODO  targetPhoneNumber
                .add("Body", "Hello this is a message through twilio")
                .build();

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .header("Authorization ", base64EncodedCredentials)
                .url(postURL)
                .post(formBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //print errors if code is not 200
            }
        });

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Upvotes: 2

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

It is not recommended to embed your Account SID and Auth Token within an application as an attacker could decompile the application, extract your credentials and wreak havoc with your account.

We recommend creating a web application that implements the Twilio REST API, wraps up your credentials sends SMS messages for you. Then you can call your web application from your Android application and send SMS without distributing your credentials.

Check out the tutorials section of Twilio.com to see how to build the server side component here. This tutorial on SMS notifications might be a good start.

Upvotes: 3

Related Questions