Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue for implementing twilio api in android

Tags:

android

twilio

I want to send sms from android device using twilio api. I use that code -

 TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

    params = new HashMap<String,String>();
    params.put("From","+1 205-490-2571");
    params.put("To","9614549499");
    params.put("Body", "Bad news , the server is down and it needs your help");

    Account acct = client.getAccount();
    SmsFactory smsFactory = acct.getSmsFactory();
    Sms sms = null;
    try {
        sms = smsFactory.create(params);
    } catch (TwilioRestException e) {


e.printStackTrace();
}

I am getting error to initialize TwilioRestClient object. I am getting this error

10-29 10:07:48.511: E/AndroidRuntime(354): FATAL EXCEPTION: main
10-29 10:07:48.511: E/AndroidRuntime(354): java.lang.NoSuchMethodError: org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.<init>
10-29 10:07:48.511: E/AndroidRuntime(354):  at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:139)
10-29 10:07:48.511: E/AndroidRuntime(354):  at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:110)
10-29 10:07:48.511: E/AndroidRuntime(354):  at com.twilio.twiliomessage.TwilioActivity$1.onClick(TwilioActivity.java:81)
10-29 10:07:48.511: E/AndroidRuntime(354):  at android.view.View.performClick(View.java:2485)
10-29 10:07:48.511: E/AndroidRuntime(354):  at android.view.View$PerformClick.run(View.java:9080)
10-29 10:07:48.511: E/AndroidRuntime(354):  at android.os.Handler.handleCallback(Handler.java:587)
10-29 10:07:48.511: E/AndroidRuntime(354):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 10:07:48.511: E/AndroidRuntime(354):  at android.os.Looper.loop(Looper.java:123)
10-29 10:07:48.511: E/AndroidRuntime(354):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-29 10:07:48.511: E/AndroidRuntime(354):  at java.lang.reflect.Method.invokeNative(Native Method)
10-29 10:07:48.511: E/AndroidRuntime(354):  at java.lang.reflect.Method.invoke(Method.java:507)
10-29 10:07:48.511: E/AndroidRuntime(354):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-29 10:07:48.511: E/AndroidRuntime(354):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-29 10:07:48.511: E/AndroidRuntime(354):  at dalvik.system.NativeStart.main(Native Method)
like image 792
Raju Sen Avatar asked Nov 11 '22 19:11

Raju Sen


1 Answers

I work on the Developer Network team at Twilio as well and building off what Devin said, including your auth token would be a big security risk. Instead, we ask that you use a TwiML app in order to receive a capability token. TwiML is a pretty intuitive language and if you’re interested, we have some examples online. You can use the capability token to sign any communications your device makes to the Twilio server. You’ll need to register your TwiML app with Twilio here.

If you’re using an Android device, you should use our Android SDK to communicate with the server. Here’s an example of a java class that you could use that uses the Twilio Android SDK. You can reference the methods in this class in your app to connect to the server. (i.e connect() and disconnect() ).

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.widget.ProgressBar;
import com.twilio.client.Connection;
import com.twilio.client.Device;
import com.twilio.client.DeviceListener;
import com.twilio.client.Twilio;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import java.util.HashMap;
import java.util.Map;


public class TwilioAndroid implements Twilio.InitListener
{
   private Device mDevice;
   private String TAG = "TwilioAndroid";
   private Connection mConnection;
   private Context mContext;

    public TwilioAndroid(Context context)
   {
       this.mContext = context;
       Twilio.initialize(context, this);
   }
//this will authenticate your device by going to the capability URL you’ve provided //(https://www.your-web-app.com/token) and retrieving a capability token to sign further communications with. 
   @Override
   public void onInitialized(){
       Log.d(TAG, "Twilio SDK is ready");
       new HttpHandler(){
           @Override
           public HttpUriRequest getHttpRequestMethod(){
               Log.d(TAG, mContext.getString(R.string.app_capability_url));

               return new HttpGet(mContext.getString(R.string.app_capability_url));
           }

           @Override
           public void onResponse(String token) {
               mDevice = Twilio.createDevice(token, null);
               Log.d(TAG, "Capability token: " + token);
           }
       }.execute();
   }

   /* Twilio.InitListener method */
   @Override
   public void onError(Exception e) {
       Log.e(TAG, "Twilio SDK couldn't start: " + e.getLocalizedMessage());
   }

//You can add more parameters to this custom method if necessary
   public void connect(String phoneNumber)
   {
       Map<String, String> parameters = new HashMap<String, String>();
       parameters.put("To", phoneNumber);
//and you can add your other parameters here 
       mConnection = mDevice.connect(parameters, null);
       if (mConnection == null)
           Log.w(TAG, "Failed to create new connection");
   }
   public void disconnect()
   {
       if (mConnection != null) {
           mConnection.disconnect();
           mConnection = null;
       }
   }

   @Override
   protected void finalize()
   {
       if (mDevice != null)
           mDevice.release();
   }
}
like image 172
sraval Avatar answered Nov 15 '22 05:11

sraval