Saturday, April 5, 2014

How to Send SMS with Parlay Rest

OMA provide specification for communication with telecom nodes, one of them is parlay rest to send sms.

Following is java sample to send sms following Parlay Rest specification

package com.test.rest.sms;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class RestSms {

Private URL sendSmsUrl = new URL("http://192.168.0.1:8080/rest/sms/messages");

public void sendSms() {

try {
HttpURLConnection connection =   (HttpURLConnection)sendSmsUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
        connection.connect();
//For simplicity we write JSON String format, we can use any library for this

String toAddress = "\"addresses\": [\"tel:666666666\"]";
String fromAddress = "\"senderName\": \"tel:321\"";
String msg Text = "\"message\": \"hello world\"";
String sms                 = "{"+toAddress+","+fromAddress+","+msgText+"}";

OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write( sms );
wr.flush();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
    while ((line = rd.readLine()) != null) {
        // Process line... and output response code
    }
    wr.close();
    rd.close();
                        
    connection.disconnect();
                        
        } catch (Exception e) {
e.printStackTrace();
        }//EO try-catch
   }

}

No comments: