0
votes

I am trying to send sms to a number and it works perfectly fine. This is my code.

public class SendSMSExample {

public  void sendSMS(String data) throws Exception {

String url = "https://api.twilio.com/2010-04-01/Accounts/XXXXXXXXXXXXXXX/Messages";
    URL url1 = new URL(url);
    URLConnection conn = url1.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");

        wr.write("data");

     TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
        Account account = client.getAccount();

        //Use the API to send a text message
        SmsFactory smsFactory = account.getSmsFactory();
        Map<String, String> smsParams = new HashMap<String, String>();
        smsParams.put("To", "+1408XXXXXXX"); 
        smsParams.put("From", "(508)XXX-XXXX"); // Replace with a Twilio phone number in your account
        smsParams.put("Body", "Hello");
        try
        {
            Sms sms = smsFactory.create(smsParams);
        }
        catch (TwilioRestException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    // Create an instance of the SmsFactory class.

        wr.flush();


     // Get the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String output = "";
        String inputLine;
        while ((inputLine = reader.readLine()) != null)
        {
            output += inputLine;
        }

        wr.close();
        reader.close();


}

}

But when I put the same code in my project on which I usually work on, I get exceptions. Following is code with exceptions

public class SendMessage extends TaskAdapter {

public static final String ACCOUNT_SID = "XXXXXXXXXXXXXXX";
public static final String AUTH_TOKEN = "XXXXXXXXXXXXXXX";

public static String PHONE_NUMBER = "from";
public static String MESSAGE = "message";

public void run(JSONObject x, JSONObject xx,
        JSONObject xxx, JSONObject xxxx) throws Exception {

    String from_phone_number = getStringValue(x, xx,
            xxx, xxxx);

    String message = getStringValue(x, xx,
            xxx, xxxx));

    String url = "https://api.twilio.com/2010-04-01/Accounts/XXXXXXXXXXXXXXX/Messages";
    URL url1 = new URL(url);
    URLConnection conn = url1.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(),
            "UTF-8");

    wr.write("data");
    TwilioRestClient client = null;
    try {
        ***client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);***
    } catch (Exception e) {
        System.out.println(e);
        e.getStackTrace();
    }
    Account account = client.getAccount();

    // Use the API to send a text message
    SmsFactory smsFactory = account.getSmsFactory();
    Map<String, String> smsParams = new HashMap<String, String>();
    smsParams.put("To", XXXX);
    smsParams.put("From", XXXX); // Replace with a Twilio phone
                                                // number in your account
    smsParams.put("Body", "Hello 123");
    try {
        Sms sms = smsFactory.create(smsParams);
    } catch (TwilioRestException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Create an instance of the SmsFactory class.

    wr.flush();

    // Get the response
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            conn.getInputStream()));
    String output = "";
    String inputLine;
    while ((inputLine = reader.readLine()) != null) {
        output += inputLine;
    }

    wr.close();
    reader.close();

}

}

I am getting exception at this line client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); I am using this jar version twilio-java-sdk-3.4.6-jar-with-dependencies And the exception is:

java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.(Ljava/lang/String;ILorg/apache/http/conn/scheme/SchemeSocketFactory;)V at com.twilio.sdk.AppEngineClientConnectionManager.(AppEngineClientConnectionManager.java:53) at com.twilio.sdk.TwilioRestClient.(TwilioRestClient.java:144) at com.twilio.sdk.TwilioRestClient.(TwilioRestClient.java:117) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:368) at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:351) at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:97) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:485) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

Thanks in advance :)

1
You should XXX out the ACCOUNT_SID and AUTH_TOKEN and any other unique keys. Did you include all the proper libs? This might be of use: stackoverflow.com/questions/17564775/…Ryan
I did XX them out and used proper libraries. Not yet resolved.kona B

1 Answers

0
votes

This is my code for sending an SMS message. I don't understand why you need a URLConnection, OutputStreamWriter, etc. in your code.

public static void sendSms(String phoneNumber, String message) throws TwilioRestException {

    SmsFactory smsFactory = account.getSmsFactory();
    HashMap<String, String> smsParams = new HashMap<String, String>();
    smsParams.put("To", myPhoneNumber); 
    smsParams.put("From", phoneNumber);
    smsParams.put("Body", message);
    smsFactory.create(smsParams);
}