Whispir Forums

Getting Started

RSS Feed

Sending an SMS using Java

  1. Hi all,

    I've just had a request about doing this in Java. Now there are a bunch of different plugins that you can use to do this in Java e.g.

    Most of these will do exactly the same thing in different ways. This post is aiming to show you how you could use any of them to fire off an SMS message using the Whispir API.

    This example can be compiled into a JAR file and run with 3 parameters:

    java -jar WhispirApi.jar "0400000000" "SUBJECT OF THE SMS" "BODY OF THE SMS"

    If you need the Maven pom.xml for this, you can download it here. If you don't know what a pom.xml file is, go here immediately!

    1. package com.whispir.test;
    2.  
    3. import java.io.IOException;
    4.  
    5. import org.apache.commons.httpclient.HttpClient;
    6. import org.apache.commons.httpclient.HttpException;
    7. import org.apache.commons.httpclient.HttpStatus;
    8. import org.apache.commons.httpclient.UsernamePasswordCredentials;
    9. import org.apache.commons.httpclient.auth.AuthScope;
    10. import org.apache.commons.httpclient.methods.PostMethod;
    11. import org.json.JSONException;
    12. import org.json.JSONObject;
    13.  
    14. public class WhispirAPI {
    15.  
    16.     public static final String API_HOST = "api.whispir.com";
    17.     public static final String API_URL = "https://api.whispir.com/messages?apikey=";
    18.     public static final String API_KEY = "";
    19.     public static final String WHISPIR_USERNAME = "";
    20.     public static final String WHISPIR_PASSWORD = "";
    21.  
    22.     /**
    23.      *
    24.      * @param args[0] String - The recipient contact information e.g. 0400000000
    25.      * @param args[1] String - The Subject of the message, or first line of the SMS
    26.      * @param args[2] String - The Content of the message, or remainder of the SMS
    27.      */
    28.     public static void main(String[] args) {
    29.  
    30.         if (args.length < 3) {
    31.             System.out.println("Three String arguments are required.");
    32.             System.out.println("1 - Phone Number. e.g. 0402859178");
    33.             System.out.println("2 - Subject. e.g. Hello World");
    34.             System.out.println("3 - Content. e.g. SMS is fun");
    35.         } else {
    36.             final String recipient = args[0];
    37.             final String subject = args[1];
    38.             final String content = args[2];
    39.  
    40.             try {
    41.                 String myString = new JSONObject().put("to", recipient)
    42.                         .put("subject", subject).put("body", content)
    43.                         .toString();
    44.  
    45.                 String response = httpPost(myString);
    46.  
    47.                 System.out.println(response);
    48.             } catch (JSONException e) {
    49.                 e.printStackTrace();
    50.             } catch (Exception e) {
    51.                 e.printStackTrace();
    52.             }
    53.  
    54.             System.out.println("Done!");
    55.         }
    56.  
    57.     }
    58.  
    59.     @SuppressWarnings("deprecation")
    60.     public static String httpPost(String jsonContent) throws Exception {
    61.         String response = "";
    62.  
    63.         // Create an instance of HttpClient.
    64.         HttpClient client = new HttpClient();
    65.  
    66.         // Create a method instance.
    67.         PostMethod method = new PostMethod(API_URL + API_KEY);
    68.  
    69.         client.getState().setCredentials(
    70.                 new AuthScope(API_HOST, 443, null),
    71.                 new UsernamePasswordCredentials(WHISPIR_USERNAME,
    72.                         WHISPIR_PASSWORD));
    73.  
    74.         method.setRequestHeader("Content-Type",
    75.                 "application/vnd.whispir.message-v1+json");
    76.         method.setRequestHeader("Accept",
    77.                 "application/vnd.whispir.message-v1+json");
    78.  
    79.         method.setDoAuthentication(true);
    80.  
    81.         method.setRequestBody(jsonContent);
    82.  
    83.         try {
    84.             // Execute the method.
    85.             int statusCode = client.executeMethod(method);
    86.  
    87.             if (statusCode != HttpStatus.SC_ACCEPTED) {
    88.                 System.err.println("Method failed: " + method.getStatusLine());
    89.             }
    90.  
    91.             // Read the response body.
    92.             byte[] responseBody = method.getResponseBody();
    93.  
    94.             // Deal with the response.
    95.             // Use caution: ensure correct character encoding and is not binary
    96.             // data
    97.             response = new String(responseBody);
    98.  
    99.         } catch (HttpException e) {
    100.             System.err.println("Fatal protocol violation: " + e.getMessage());
    101.             e.printStackTrace();
    102.             response = "error";
    103.         } catch (IOException e) {
    104.             System.err.println("Fatal transport error: " + e.getMessage());
    105.             e.printStackTrace();
    106.             response = "error";
    107.         } finally {
    108.             // Release the connection.
    109.             method.releaseConnection();
    110.         }
    111.  
    112.         return response;
    113.     }
    114. }
    115.  

    Now, I realise that I have used a deprecated method to set my content body, but I'm happy to do that for an example. This should be able to be used by most developers to get started with the basic flow of how to POST a call to Whispir using any of these plugins.

    If you need further assistance on getting this set up, please contact apisupport@whispir.com.

    Enjoy!

    Jordan Walsh

    Message edited by Jordan Walsh 5 years ago

[ Page 1 of 1 ]