我必须实现一个方法,在这个方法中,我将两个参数作为Mobile传递,第二个参数是要发送的消息。
我第一次使用clickatell,它成功地发送了消息,但它是为它编写代码的正确方法吗?
这是我发布的测试代码,我可以在我的生产代码中实现这一点。
我已经清空了钥匙,换成了星星。
作为一个付费服务,我应该去点击或有任何其他选择发送短信,我可以尝试。
请检查代码。
public class Sms {
public final String APIKEY = "*************************";
//private String mobile;
public void sms_generation(String mobile, String Message) {
System.out.println("\n\nSTARTING WITH TESTING REST:");
// Create New object (Assign auth straight away.):
ClickatellRest click = new ClickatellRest(APIKEY);
// We cannot test for auth, so lets start with balance:
System.out.println("TESTING GET BALANCE");
try {
double response = click.getBalance();
System.out.println(response);
} catch (UnknownHostException e) {
System.out.println("Host could not be found");
} catch (Exception e) {
System.out.println(e.getMessage());
}
// Assuming the auth was successful, lets send one message, to one
// recipient:
System.out.println("\nTESTING SEND SINGLE MESSAGE");
try {
ClickatellRest.Message response = click.sendMessage(mobile, Message);
System.out.println(response);
if (response.error != null) {
System.out.println(response.error);
} else {
ClickatellRest.Message msg = click
.getMessageStatus(response.message_id);
System.out.println("ID:" + msg.message_id);
System.out.println("Status:" + msg.status);
System.out.println("Status Description:" + msg.statusString);
System.out.println("Charge:" + msg.charge);
System.out.println("\nTESTING STOP MESSAGE");
ClickatellRest.Message msgStatus = click
.stopMessage(response.message_id);
System.out.println("ID:" + msgStatus.message_id);
System.out.println("Status:" + msgStatus.status);
System.out.println("Status Description:"
+ msgStatus.statusString);
}
} catch (UnknownHostException e) {
System.out.println("Host could not be found");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
发布于 2017-06-22 10:57:01
使用Logger,不要打印所有的应用程序信息,在生产中它会破坏服务器日志。您可以使用不同的日志级别(调试、信息)。例如,在prod中使用信息级别,在dev环境中使用调试级别。
2不要捕获服务层中的所有异常,您可以将它们抛到控制器(如果使用spring )并捕获所有这些异常。
https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
3试着保持代码的整洁,以便于阅读。
public void sendMessage(String mobile, String Message) {
ClickatellRest click = new ClickatellRest(APIKEY);
ClickatellRest.Message response = click.sendMessage(mobile, Message);
log.debug(response);
if (response.error == null) {
ClickatellRest.Message msg = click.getMessageStatus(response.message_id);
log.debug(msg);
ClickatellRest.Message msgStatus = click.stopMessage(response.message_id);
log.debug(msgStatus);
} else {
log.error(response.err);
}
}
4如果可以的话,如果经常记录这个对象,在ClickatellRest.Message类中重写ClickatellRest.Message()方法。
https://codereview.stackexchange.com/questions/166387
复制相似问题