我试图使用java发送短信,为此我首先尝试使用thilio。但我得到了底部提到的错误。然后我下载,在nexmo中创建了一个帐户,但我再次收到同样的错误。请任何有过的人已经在上面两个工作过可以告诉我为什么会出现这个问题。
import com.nexmo.messaging.sdk.NexmoSmsClient;
import com.nexmo.messaging.sdk.SmsSubmissionResult;
import com.nexmo.messaging.sdk.messages.TextMessage;
public class SendTextMessage {
public static final String API_KEY = "apikey";
public static final String API_SECRET = "apisecret";
public static final String SMS_FROM = "singapore_number";// as i use my office number so can not disclose it
public static final String SMS_TO = "singapre_number";
public static final String SMS_TEXT = "Hello World!";
public static void main(String[] args) {
// Create a client for submitting to Nexmo
NexmoSmsClient client = null;
try {
client = new NexmoSmsClient(API_KEY, API_SECRET);
} catch (Exception e) {
System.err.println("Failed to instanciate a Nexmo Client");
e.printStackTrace();
throw new RuntimeException("Failed to instanciate a Nexmo Client");
}
// Create a Text SMS Message request object ...
TextMessage message = new TextMessage(SMS_FROM, SMS_TO, SMS_TEXT);
// Use the Nexmo client to submit the Text Message ...
SmsSubmissionResult[] results = null;
try {
results = client.submitMessage(message);
} catch (Exception e) {
System.err.println("Failed to communicate with the Nexmo Client");
e.printStackTrace();
throw new RuntimeException("Failed to communicate with the Nexmo Client");
}
// Evaluate the results of the submission attempt ...
System.out.println("... Message submitted in [ " + results.length + " ] parts");
for (int i=0;i<results.length;i++) {
System.out.println("--------- part [ " + (i + 1) + " ] ------------");
System.out.println("Status [ " + results[i].getStatus() + " ] ...");
if (results[i].getStatus() == SmsSubmissionResult.STATUS_OK)
System.out.println("SUCCESS");
else if (results[i].getTemporaryError())
System.out.println("TEMPORARY FAILURE - PLEASE RETRY");
else
System.out.println("SUBMISSION FAILED!");
System.out.println("Message-Id [ " + results[i].getMessageId() + " ] ...");
System.out.println("Error-Text [ " + results[i].getErrorText() + " ] ...");
if (results[i].getMessagePrice() != null)
System.out.println("Message-Price [ " + results[i].getMessagePrice() + " ] ...");
if (results[i].getRemainingBalance() != null)
System.out.println("Remaining-Balance [ " + results[i].getRemainingBalance() + " ] ...");
}
}
}
错误如下
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
at SendTextMessage.main(SendTextMessage.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 1 more
第 32 行表示这一行 client = new NexmoSmsClient(API_KEY, API_SECRET);