Java code to send sms and check credit

You can also download the class here - Code provided by Liman


  1. import java.io.BufferedReader;
  2. import java.io.DataOutputStream;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.net.URLEncoder;
  8. import java.util.logging.Logger;
  9. import org.json.me.JSONObject;
  10. /**
  11. * NasaraServerUtil class - To be used for sending sms and checking sms credit
  12. *
  13. */
  14. public class NasaraAPIUtil {
  15. private static String apiKey = "xxxxxxxxxxxxxxxxx";
  16. private static String sendSMS_API_URL = "http://sms.nasaramobile.com/api?";
  17. private static String checkCredit_API_URL = "http://sms.nasaramobile.com/api/accounts/credit?";
  18. private static final Logger log = Logger.getLogger(NasaraAPIUtil.class.getName());
  19. //method to send sms
  20. public static String sendSMS(String senderId,String receiversPhone,String message) {
  21. URL url;
  22. HttpURLConnection connection = null;
  23. String urlParameters = "api_key="+apiKey +"&sender_id="+senderId+"&phone="+receiversPhone+"&message="+URLEncoder.encode(message);
  24. try {
  25. //Create connection
  26. url = new URL(sendSMS_API_URL);
  27. connection = (HttpURLConnection) url.openConnection();
  28. connection.setRequestMethod("POST");
  29. connection.setRequestProperty("Content-Length", ""
  30. + Integer.toString(urlParameters.getBytes().length));
  31. connection.setRequestProperty("Content-Language", "en-US");
  32. connection.setUseCaches(false);
  33. connection.setDoInput(true);
  34. connection.setDoOutput(true);
  35. log.info(sendSMS_API_URL);
  36. log.info("Sending request with :" + urlParameters);
  37. //Send request
  38. DataOutputStream wr = new DataOutputStream(
  39. connection.getOutputStream());
  40. wr.writeBytes(urlParameters);
  41. wr.flush();
  42. wr.close();
  43. //Get Response
  44. InputStream is = connection.getInputStream();
  45. BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  46. String line;
  47. StringBuffer response = new StringBuffer();
  48. while ((line = rd.readLine()) != null) {
  49. response.append(line);
  50. response.append('\r');
  51. }
  52. rd.close();
  53. log.info("Response from Nasara API Server:" + response.toString());
  54. return getResponse(Integer.parseInt(response.toString()));
  55. } catch (Exception e) {
  56. log.severe("Error: " + e.getMessage());
  57. return null;
  58. } finally {
  59. if (connection != null) {
  60. connection.disconnect();
  61. }
  62. }
  63. }
  64. // method to check sms credit
  65. public static String checkCredit() {
  66. URL url;
  67. HttpURLConnection connection = null;
  68. String urlParameters = "api_key="+apiKey ;
  69. try {
  70. //Create connection
  71. url = new URL(checkCredit_API_URL);
  72. connection = (HttpURLConnection) url.openConnection();
  73. connection.setRequestMethod("POST");
  74. connection.setRequestProperty("Content-Length", ""
  75. + Integer.toString(urlParameters.getBytes().length));
  76. connection.setRequestProperty("Content-Language", "en-US");
  77. connection.setUseCaches(false);
  78. connection.setDoInput(true);
  79. connection.setDoOutput(true);
  80. log.info(checkCredit_API_URL);
  81. log.info("Sending request with :" + urlParameters);
  82. //Send request
  83. DataOutputStream wr = new DataOutputStream(
  84. connection.getOutputStream());
  85. wr.writeBytes(urlParameters);
  86. wr.flush();
  87. wr.close();
  88. //Get Response
  89. InputStream is = connection.getInputStream();
  90. BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  91. String line;
  92. StringBuffer response = new StringBuffer();
  93. while ((line = rd.readLine()) != null) {
  94. response.append(line);
  95. response.append('\r');
  96. }
  97. rd.close();
  98. log.info("Response from Nasara API Server:" + response.toString());
  99. JSONObject rJson = new JSONObject(response.toString());
  100. String responseCode = rJson.getString("status");
  101. if (responseCode.equals("1000")) {
  102. return rJson.getString("data");
  103. }else{
  104. return getResponse(Integer.parseInt(response.toString()));
  105. }
  106. } catch (Exception e) {
  107. log.severe("Error: " + e.getMessage());
  108. return null;
  109. } finally {
  110. if (connection != null) {
  111. connection.disconnect();
  112. }
  113. }
  114. }
  115. //response translation of response codes
  116. public static String getResponse(int errorCode) {
  117. switch (errorCode) {
  118. case 1801:
  119. return "message sent successfully";
  120. case 1802:
  121. return "message sending failed";
  122. case 1803:
  123. return "invalid login phone or password given";
  124. case 1804:
  125. return "not enough sms credit";
  126. case 1805:
  127. return "sender id must be more than 1 charater and less than 12 characters";
  128. case 1806:
  129. return "phone number must be more than 8 characters";
  130. case 1903:
  131. return "there was an error";
  132. case 1902:
  133. return "this api key does not exist";
  134. case 1901:
  135. return "api key must be posted to execute request";
  136. }
  137. return "server Error";
  138. }
  139. }