获取ArrayIndexOutOfBoundsException

时间:2013-11-26 10:18:22

标签: java indexoutofboundsexception

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");   
    }


}

这里我在代码

中得到了一个Exception
String[] r2=retArray[1].split(" ");     

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

任何人都可以解决此问题。 在此先感谢。

2 个答案:

答案 0 :(得分:0)

可能你的电话有例外。你正在捕捉它,但随后无所事事。执行System.out.println(结果)并查看HTTP调用的结果。

答案 1 :(得分:0)

您不会提供例外目录,因此解决此问题的复杂性

可能有助于像这样更改此代码

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

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