我正在尝试在我的 android 应用程序中包含 Twilio API,但是当我尝试从中发送短信时,我收到了 java.lang.NoSuchMethodError。我试图通过添加 httpclient-4.2.2.jar 文件来解决它,但没有成功。
以下是我的错误日志:
12-06 10:32:26.497: E/AndroidRuntime(12901): FATAL EXCEPTION: main
12-06 10:32:26.497: E/AndroidRuntime(12901): java.lang.NoSuchMethodError: org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.<init>
12-06 10:32:26.497: E/AndroidRuntime(12901): at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:137)
12-06 10:32:26.497: E/AndroidRuntime(12901): at com.example.twiliodemo.TwilioDemo.sendMessageByTwilio(TwilioDemo.java:40)
12-06 10:32:26.497: E/AndroidRuntime(12901): at com.example.twiliodemo.TwilioDemo.onCreate(TwilioDemo.java:23)
12-06 10:32:26.497: E/AndroidRuntime(12901): at android.app.Activity.performCreate(Activity.java:5008)
12-06 10:32:26.497: E/AndroidRuntime(12901): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-06 10:32:26.497: E/AndroidRuntime(12901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-06 10:32:26.497: E/AndroidRuntime(12901): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-06 10:32:26.497: E/AndroidRuntime(12901): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-06 10:32:26.497: E/AndroidRuntime(12901): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-06 10:32:26.497: E/AndroidRuntime(12901): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 10:32:26.497: E/AndroidRuntime(12901): at android.os.Looper.loop(Looper.java:137)
12-06 10:32:26.497: E/AndroidRuntime(12901): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-06 10:32:26.497: E/AndroidRuntime(12901): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 10:32:26.497: E/AndroidRuntime(12901): at java.lang.reflect.Method.invoke(Method.java:511)
12-06 10:32:26.497: E/AndroidRuntime(12901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-06 10:32:26.497: E/AndroidRuntime(12901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-06 10:32:26.497: E/AndroidRuntime(12901): at dalvik.system.NativeStart.main(Native Method)
这是我的活动。
package com.example.twiliodemo;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.resource.factory.SmsFactory;
import com.twilio.sdk.resource.instance.Account;
import com.twilio.sdk.resource.instance.Sms;
public class TwilioDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendMessageByTwilio();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendMessageByTwilio(){
/* Find your sid and token at twilio.com/user/account */
try {
String ACCOUNT_SID = "AC15b63878d56d436af26cfd6f0e94c706";
String AUTH_TOKEN = "2c870b5aa3c5f4d1bcacbe747d6808f6";
String TWILIO_URL="http://api.twilio.com";
TwilioRestClient clientTwilio=new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN,TWILIO_URL);
Account account = clientTwilio.getAccount();
SmsFactory smsFactory = account.getSmsFactory();
Map<String, String> smsParams = new HashMap<String, String>();
smsParams.put("To", "+919423872793");
smsParams.put("From", "+12403486854"); // Replace with a valid phone
// number in your account
smsParams.put("Body", "Hi..");
Sms sms = smsFactory.create(smsParams);
Log.d("SMS -", ""+sms);
} catch (Exception e) {
Log.e("**************", String.valueOf(e));
}
}
}
我还在 libs 文件夹中添加了“twilio-java-sdk-3.3.10.jar”。