简体   繁体   中英

Sending SMS through low end API

I have a message constructor method of the form:

public static String constructMsg(CustomerInfo customer) {
    ... snipped 
    String msg = String.format("Snipped code encapsulated by customer object");

    return msg;
}

The API link is:

http://xxx.xxx.xx.xx:8080/bulksms?username=xxxxxxx &password=xxxx &type=0 &dlr=1&destination=10digitno & source=xxxxxx& message=xxxxx

In my main method I have(s):

List<CustomerInfo> customer = dao.getSmsDetails(userDate);

        theLogger.info("Total No : " + customer.size() );

        if (!customer.isEmpty()) {

            for (CustomerInfo cust : customer) {
                String message = constructMsg(cust);

                // Add link and '?' and query string
                // use URLConnection's connect method 
            }
        }

So I am using connect method of URLConnection. The API does not have any documentation. Is there any way for checking response?

My other question is, I have been advised to use ThreadPoolExecutor. How would I use use it here?

This method use HTTPURLConnection to perform a GET request returning the response as a String. There're many way to do it, this is not particularly brilliant but it's really readable.

public String getResponse(String url, int timeout) {
    HttpURLConnection c;
    try {
        URL u = new URL(url);
        c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setRequestProperty("Content-length", "0");
        c.setUseCaches(false);
        c.setAllowUserInteraction(false);
        c.setConnectTimeout(timeout);
        c.setReadTimeout(timeout);
        c.connect();
        int status = c.getResponseCode();

        switch (status) {
            case 200:
            case 201:
                BufferedReader br = new BufferedReader(new              InputStreamReader(c.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line+"\n");
                }
                br.close();
                return sb.toString();
       default:
        return "HTTP CODE: "+status;
        }

    } catch (MalformedURLException ex) {
        Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);
    } finally{
       if(c!=null) c.disconnect();
    }
    return null;
}

Call this method like this:

getResponse("http://xxx.xxx.xx.xx:8080/bulksms?username=xxxxxxx&password=xxxx&type=0 &dlr=1&destination=10digitno&source=xxxxxx&message=xxxxx",2000);

I assume the whitespaces in your URL are not supposed to be there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM