-1
public class CSoftHttpClientSMSService extends SMSService     {

    private HttpClient client;    
    //private String mmsServiceUrl = "https://www.csoft.co.uk/sendmms";    
    private String smsServiceUrl = "https://www.csoft.co.uk/sendsms";    
    private String username = "my_username"; //pass the username   
            private String PIN = "my_password"; //pass the pin from the website

       //private static Log log = LogFactory.getLog(CSoftHttpClientSMSService.class);
      public CSoftHttpClientSMSService() {
        super();
        client = new HttpClient();
    }  

    /* 
     * (non-Javadoc)
     * 
     * @see com.ipanema.support.sms.SMSService#getAccountMessageLimit()
     */
        public int getAccountMessageLimit()     {
            //log.debug("in getMessageLimit");
                PostMethod method = new PostMethod(smsServiceUrl);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));
        method.setParameter("Username", username);
        method.setParameter("PIN", PIN);
        method.setParameter("AvailableMessages", "");
        String result = new String();
        try {
            result = doHttpServiceRequest(method);
            //log.debug("result is: " + result);
        } catch (Exception e) {
            //log.warn(e.toString());
        }
           String[] retArray = result.split("=");
           return Integer.valueOf(retArray[1]);
    }


    /*
     * (non-Javadoc)
     * 
     * @see com.ipanema.support.sms.SMSService#getAccountBalanceCurrency()
     */
    public String getAccountBalanceCurrency()     {
        //log.debug("in getAccountBalanceCurrency");
        PostMethod method = new PostMethod(smsServiceUrl);
        //method.getParams().setParameter(HttpMethodParams.COOKIE_POLICY,   new DefaultHttpMethodRetryHandler(3, false));
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        method.setParameter("Username", username);
        method.setParameter("PIN", PIN);
        method.setParameter("AvailableCredit", "");
        String result = new String();
        try {
            result = doHttpServiceRequest(method);
        //  log.debug("result is: " + result);
        } catch (Exception e) {
            //log.warn(e.toString());
        }
        String[] retArray = result.split("=");
        String[] r2 = retArray[1].split(" ");
        return r2[0];
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ipanema.support.sms.SMSService#getAccountBalance()
     */
    public float getAccountBalance()     {
        //log.debug("in getAccountBalance");
        PostMethod method = new PostMethod(smsServiceUrl);    

        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        method.setParameter("Username", username);    
        method.setParameter("PIN", PIN);   
        method.setParameter("AvailableCredit", "");   
        String result = new String();   
        try {
            result = doHttpServiceRequest(method);   
        System.out.println("result is: " + result);   
        } catch (Exception e) {  
        //  log.warn(e.toString());   
        }


        String[] retArray = result.split("=");   
        String[] r2=retArray[1].split(" ");  
        System.out.println("Outside");   
        //return Float.valueOf(r2[0]);    
         return Float.valueOf(retArray[0]);    

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ipanema.support.sms.SMSService#sendSimpleSMS(java.lang.String,
     *      java.lang.String)
     */
    public void sendSimpleSMS(String msgTo, String msgBody)     {
        //  log.debug("in sendSimpleSMS");
            PostMethod method = new PostMethod(smsServiceUrl);    

            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));    
            method.setParameter("Username", username);   
            method.setParameter("PIN", PIN);    
            method.setParameter("SendTo", msgTo);   
            method.setParameter("Message", msgBody);   
    //      System.out.println("done");   
            try {   
                String result = doHttpServiceRequest(method);   
                //log.debug("result is: " + result);  
            } catch (Exception e) {   
                //log.warn(e.toString());
            }
        }


    private String doHttpServiceRequest(PostMethod method)
            throws HttpException, IOException     {
        //log.debug("in doHttpServiceRequest");
        int statusCode = client.executeMethod(method);   
        if (statusCode != HttpStatus.SC_OK) {
            //log.info("Method failed: " + method.getStatusLine());
        }    
        InputStream is = method.getResponseBodyAsStream();   
        BufferedReader br = new BufferedReader(new InputStreamReader(is));  
        String line = null;
        StringBuffer response = new StringBuffer();  
        while ((line = br.readLine()) != null) {  
            response.append(line);  
        }
        method.releaseConnection();
        return response.toString();
    }

    public static void main(String args[])     {
    //  log.debug("in main");
        SMSService sms = new CSoftHttpClientSMSService();   
        sms.getAccountBalanceCurrency();  
        sms.getAccountBalance();  
        int limitBefore = sms.getAccountMessageLimit();  
    //  log.info(limitBefore);
//      sms.sendWAPPushSMS("+316xxxxxxxx", "http://www.google.com", "subject");   
        sms.sendSimpleSMS("+918867843591", "this is a simple sms. www.google.com www.yahoo.co.uk");
//      sms.sendFlashSMS("+316xxxxxxxx", "this is a flash message. www.google.com www.yahoo.co.uk");
        int limitAfter = sms.getAccountMessageLimit();
//      log.info("operation took " + (limitBefore - limitAfter) + " units");   
    }


}

Here i am getting an Exception.in the code

String[] r2=retArray[1].split(" ");     

System.out.println("Outside");
 return Float.valueOf(retArray[0]);

can anyone resolve this issue. Thanks in Advance.

Nizam
  • 5,698
  • 9
  • 45
  • 57
Deepu
  • 11
  • 7
  • Please log and check the result. I think you are not getting an "=" in the result. – Raze Nov 26 '13 at 10:25
  • by doing that i am getting result is: MessageIdentifier=-1&Report=2&Text=Invalid%20login&Units=0 – Deepu Nov 26 '13 at 10:34
  • Which line is it crashing on, `String[] r2=retArray[1].split(" "); ` or `return Float.valueOf(retArray[0]);`? Make sure that r2 actually is an array with length >= 0... With the result as you give it, `MessageIdentifier=-1&Report=2&Text=Invalid%20login&Units=0` there is no space, and so `retArray[1].split(" ");` might not return what you think it is. – cjwirth Nov 26 '13 at 10:40
  • ok cool String[] retArray = result.split("="); //String[] r2=retArray[1].split(" "); System.out.println("Outside"); //return Float.valueOf(r2[0]); return Float.valueOf(retArray[1]); by doing like the above m getting exception like result is: MessageIdentifier=-1&Report=2&Text=Invalid%20login&Units=0 Exception in thread "main" java.lang.NumberFormatException: For input string: "-1&Report" at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Float.valueOf(Unknown Source) at SMSServiceJava.CSoftHttpClientSMSService.main(CSoftHttpClientSMSService.java:224) – Deepu Nov 26 '13 at 10:44
  • @Nizam: Invalid Login where?? – Deepu Nov 26 '13 at 10:44
  • MessageIdentifier=-1&Report=2&`Text=Invalid%20login`&Units=0 – Nizam Nov 26 '13 at 10:51

2 Answers2

0

Probbably there's an exception in your call. You are catching it, but then doing nothing with it. Do a System.out.println(result) and see what is the result of your HTTP call.

spakendralo man
  • 635
  • 2
  • 8
  • 21
  • result is: MessageIdentifier=-1&Report=2&Text=Invalid%20login&Units=0 it is showing like this...the result variable. – Deepu Nov 26 '13 at 10:32
0

You are not give exception catalog so its complicate to resolve this issue

may be it help change this code like this

String[] r2=retArray[1].split(" ");     

System.out.println("Outside");
 return Float.valueOf(retArray[1]);
  • By doing this i am getting an exception called ArrayIndexOfBoundsException. – Deepu Nov 26 '13 at 10:33
  • give your exception catalog –  Nov 26 '13 at 10:35
  • @Deepu update answer try it –  Nov 26 '13 at 10:35
  • Exception in thread "main" java.lang.NumberFormatException: For input string: "MessageIdentifier" Outside at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Float.valueOf(Unknown Source) at SMSServiceJava.CSoftHttpClientSMSService.getAccountBalance(CSoftHttpClientSMSService.java:111) at SMSServiceJava.CSoftHttpClientSMSService.main(CSoftHttpClientSMSService.java:224) – Deepu Nov 26 '13 at 10:38
  • This is not ArrayIndexOfBoundsException, this is NumberFormatException that you are getting because you are trying to `Float.valueOf("MessageIdentifier");` which is failing. Your retArray is not what you think it is – cjwirth Nov 26 '13 at 10:41
  • You are write @cjwirthyou –  Nov 26 '13 at 10:43
  • @Deepu just edit your Question –  Nov 26 '13 at 10:43
  • @cjwirth - can you please tell me how to resolve this?? – Deepu Nov 26 '13 at 10:51