5

我想使用 twilio api 从 android 设备发送短信。我使用该代码 -

 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();
}

我在初始化 TwilioRestClient 对象时遇到错误。我收到此错误

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)
4

1 回答 1

1

我也在 Twilio 的开发者网络团队工作,并根据 Devin 所说的来构建,包括您的身份验证令牌将是一个很大的安全风险。相反,我们要求您使用 TwiML 应用程序来接收功能令牌。TwiML 是一种非常直观的语言,如果您有兴趣,我们有一些在线示例。您可以使用功能令牌对您的设备与 Twilio 服务器进行的任何通信进行签名。您需要在此处向 Twilio 注册您的 TwiML 应用程序。

如果您使用的是 Android 设备,则应使用我们的Android SDK与服务器通信。这是一个使用 Twilio Android SDK 的 java 类示例。您可以在您的应用程序中引用此类中的方法来连接到服务器。(即 connect() 和 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();
   }
}
于 2015-10-27T19:35:30.707 回答