[英]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: API链接为:
http://xxx.xxx.xx.xx:8080/bulksms?username=xxxxxxx &password=xxxx &type=0 &dlr=1&destination=10digitno & source=xxxxxx& message=xxxxx 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. 所以我正在使用URLConnection的connect
方法。 The API does not have any documentation. 该API没有任何文档。 Is there any way for checking response? 有什么办法可以检查响应?
My other question is, I have been advised to use ThreadPoolExecutor. 我的另一个问题是,建议我使用ThreadPoolExecutor。 How would I use use it here? 我将在这里使用它吗?
This method use HTTPURLConnection to perform a GET request returning the response as a String. 此方法使用HTTPURLConnection执行GET请求,以字符串形式返回响应。 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. 我假设您网址中的空格不应存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.