我想使用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)
答案 0 :(得分:1)
我也在Twilio的开发者网络团队工作,并建立了Devin所说的内容,包括你的身份验证令牌将是一个很大的安全风险。相反,我们要求您使用TwiML应用程序以接收功能令牌。 TwiML是一种非常直观的语言,如果您感兴趣,我们会在线examples。您可以使用功能令牌对设备与Twilio服务器进行的任何通信进行签名。您需要使用Twilio here注册您的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();
}
}