我正在尝试使用在 android 中发送短信twilio
。但我无法发送它是例外
Fatel exception: java.lang.NoClassDefFoundError:com.twilio.sdk.TwilioRestClient
如果我试图在普通的 java 程序中发送短信。它工作正常。如果我在 android 中使用相同的代码,它给出了上述异常,我可以知道如何解决该异常。
并且MainActivity.java
是
package com.example.smsthroughandroid;
import java.util.HashMap;
import java.util.Map;
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.SmsFactory;
import com.twilio.sdk.resource.instance.Account;
import com.twilio.sdk.resource.instance.Sms;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button b1;
public static final String ACCOUNTSID = "AC126e8ddb3602a2ee77c1295c54b82fdc";
public static final String AUTHTOKEN = "04c75d5737fdd5cfb2d96f9678d1b4cd";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
/* Instantiate a new Twilio Rest Client */
try {
TwilioRestClient client = new TwilioRestClient(ACCOUNTSID, AUTHTOKEN);
// Get the account and call factory class
Account acct = client.getAccount();
SmsFactory smsFactory = acct.getSmsFactory();
Map<String,String> params = new HashMap<String,String>();
params.put("From","+13312096397");
params.put("To", "+918095083152");
params.put("Body", "Bad news " + "hai" + ", the server is down and it needs your help");
// send an sms a call
// ( This makes a POST request to the SMS/Messages resource)
Sms sms = smsFactory.create(params);
System.out.println("Success sending SMS: " + sms.getSid());
}
catch (TwilioRestException e) {
e.printStackTrace();
}
Toast.makeText(this, "hai", Toast.LENGTH_LONG).show();
}
}
我activity_main.xml
的是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="36dp"
android:layout_marginTop="59dp"
android:text="Button" />
</RelativeLayout>
我正在使用以下罐子
commons-httpclient-3.1.jar
gwt-twilio-1.1.jar
jackson-core-asl-1.9.13.jar
jackson-mapper-asl-1.9.0.jar
org.apache.httpcomponents.httpclient_4.3.1.jar
twilio-java-sdk-3.3.14.jar
org.apache.commons.logging.jar
twilioclient-android-1.1.2-3635733.jar