SMS API in Java – XML API – Send sms using Java

0

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import java.util.Arrays;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;

public class SMSEngine {

private static String SMS_SERVICE_URL = “http://8.6.95.137/psms/servlet/psms.Eservice2″;
private static String ADDRESS_TAG = “

“;

//Appended text private static String DATA_TAG = "{3}“;

public void sendSMS(final String from, final List toList, final String text) {

Thread t = new Thread(new Runnable() {
public void run() {
try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(SMS_SERVICE_URL);
String dataParameter = getDataParameter(from, toList, text);
method.addParameter(“data”, dataParameter);
method.addParameter(“action”, “send”);
client.executeMethod(method);
System.out.println(method.getResponseBodyAsString());
} catch (HttpException e) {
System.out.println(“Error in sending SMS.” + e.toString());
} catch (IOException e) {
System.out.println(“Error in sending SMS.” + e.toString());
}
}
});
t.start();
}

private String getDataParameter(String from, List toList, String text) {

final String userName =”“;
final String password = ““;

StringBuffer addressTags = new StringBuffer();
for (int i = 0; i < toList.size(); i++) {
String address = (String) toList.get(i);
addressTags.append(MessageFormat.format(ADDRESS_TAG, new Object[] {
from, address, new Integer(i + 1) }));
}
return MessageFormat.format(DATA_TAG, new Object[]{userName, password, text, addressTags});
}

public static String encodeHTML(String s)
{
StringBuffer out = new StringBuffer();
for(int i=0; i {
char c = s.charAt(i);
if((c >= 32 && c<=46) || (c >= 58 && c<=64) || (c >= 91 && c<=96) || (c >= 123 && c<=126))
{
out.append("&#"+(int)c+";");
}
else
{
out.append(c);
}
}
return out.toString();
}

public static void main(String args[]){
SMSEngine vd = new SMSEngine();

Object[] array = new Object[]{"destination1,destination2,xxxxxx"};
java.util.List toList = Arrays.asList(array);

vd.sendSMS("“, toList, encodeHTML(“hello <> world”));
}

}

Leave a Reply

Proudly designed by BULK SMS Gateway.