2

I am trying to send sms in android using twilio. but iam unable to send it is giving exception that

Fatel exception: java.lang.NoClassDefFoundError:com.twilio.sdk.TwilioRestClient

if iam trying to send sms in normal java program.it is working fine. if iam using same code in android it is giving above exception can i know How can i solve that exception.

and MainActivity.java is

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

    }



}

and my activity_main.xml is

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

iam using following jars

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
user2897471
  • 55
  • 1
  • 8

3 Answers3

0

You must use twilioclient-android's classes only, not twilio-java-sdk on your android app. Your imports must start with com.twilio.client.. See this

Devrim
  • 15,345
  • 4
  • 66
  • 74
  • I am using twilioclient-android-1.1.2-3635733.jar file – user2897471 Oct 26 '13 at 12:51
  • I don't see any references at your activity? As I see, you only added its jar to your app's buildpath. But you did not use it. Please remove twilio-java-sdk from your buildpath and use twilioclient-android's components. – Devrim Oct 26 '13 at 12:55
  • Iam using all jars. in normal java progrm it is working fine but in android only problem – user2897471 Oct 26 '13 at 12:59
  • It is working fine on 'normal java' because twilio-java-sdk is for 'normal java'. For android you must build your client with twilioclient-android api. You must use com.twilio.client.Device and com.twilio.client.Twilio classes at your activity. – Devrim Oct 26 '13 at 13:06
  • iam using twilioclient-android-1.1.2-3635733.jar file – user2897471 Oct 26 '13 at 13:09
  • I don't see any references at your activity for twilioclient-android? Problem is you are triying to use standart java library (twilio-java-sdk) on android. You are not using twilioclient-android only adding its jar to buildpath and that does not mean that you are using it. Please see steps for using twilio on android. http://www.twilio.com/docs/quickstart/php/android-client – Devrim Oct 26 '13 at 13:25
  • I have done it, but still sometimes i get error `BasicPhone.device.getState()` and sometimes in `device.connect(parameters,null)`. Do you also get?? – Anshul Tyagi Jun 02 '15 at 06:34
-2

You must search on the web before asking any question. please refer to the link you find your answer.

Android java.lang.NoClassDefFoundError

Community
  • 1
  • 1
  • i tested all jar files are includes – user2897471 Oct 26 '13 at 12:36
  • check them all. by clicking on the checkbox – Abdul Mohsin Oct 26 '13 at 12:38
  • i done but it is giving now FaTal Exception: java.lang.noSuchMethodError org.apache.http.impl.conn.tsccm.ThreadSafeClientManager – user2897471 Oct 26 '13 at 12:49
  • Android's internal copy of the Apache HTTP client classes is incomplete. The library you are using may depend on one of the methods that has been removed to save space. Unfortunately supplying your own is not an option because Android does not allow a user package to replace system-supplied ones. You may have to recompile the library you are using to use a different package name, or you may need to find a version specifically built for Android. – Jules Oct 26 '13 at 12:53
-2

Try this- 1) project-> config build path-> order and export-> move dependent project on top else

2) Project -> Properties -> Java Build Path -> Order & Export and ensure Android Private Libraries are checked for your project and for all other library projects you are using.

DD.
  • 973
  • 2
  • 10
  • 32