2013-10-19 108 views
0

我想發送短信使用Java和短信網關使用的是fullonsms.com,但它Giveg錯誤。請告訴我我在哪裏做錯誤。無法發送短信使用Java使用短信網關

package sms; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.List; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

public class MobileTry 
{ 
private final String LOGIN_URL = "http://fullonsms.com/home.php"; 
private final String SEND_SMS_URL = "http://fullonsms.com/home.php"; 
private final String LOGOUT_URL = "http://fullonsms.com/logout.php?LogOut=1"; 

private final int MESSAGE_LENGTH = 10; 
private final int MOBILE_NUMBER_LENGTH = 140; 
private final int PASSWORD_LENGTH = 10; 

private String mobileNo; 
private String password; 
private DefaultHttpClient httpclient; 
MobileTry(String username,String password) 
{ 
this.mobileNo = username; 
this.password = password; 
httpclient = new DefaultHttpClient(); 
} 



public boolean isLoggedIn() throws IOException { 
// User Credentials on Login page are sent using POST 
// So create httpost object 
HttpPost httpost = new HttpPost(LOGIN_URL); 

// Add post variables to login url 
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
nvps.add(new BasicNameValuePair("MobileNoLogin", mobileNo)); 
nvps.add(new BasicNameValuePair("LoginPassword", password)); 
httpost.setEntity(new UrlEncodedFormEntity(nvps)); 

// Execute request 
try{ 
HttpResponse response = this.httpclient.execute(httpost); 
HttpEntity entity = response.getEntity(); 
if (entity != null) { 
    System.out.println("entity " + slurp(entity.getContent(), 10000000)); 
    System.out.println("entity " + response.getStatusLine().getStatusCode()); 

    return true; 
} 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
    System.out.println(e); 
} 
//Check response entity 
return false; 

} 

public boolean sendSMS(String toMobile,String message) throws IOException { 
HttpPost httpost = new HttpPost(SEND_SMS_URL); 
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
nvps.add(new BasicNameValuePair("MobileNos", toMobile)); 
nvps.add(new BasicNameValuePair("Message", message)); 

httpost.setEntity(new UrlEncodedFormEntity(nvps)); 
HttpResponse response = this.httpclient.execute(httpost); 
HttpEntity entity = response.getEntity(); 
if(entity != null) { 
         System.out.println("entity " + slurp(entity.getContent(), 10000000)); 
         System.out.println("entity " + response.getStatusLine().getStatusCode()); 
    return true; 
} 
return false; 
} 

public boolean logoutSMS() throws IOException { 
HttpGet httpGet = new HttpGet(LOGOUT_URL); 
HttpResponse response; 
response = this.httpclient.execute(httpGet); 
HttpEntity entity = response.getEntity(); 
if (entity != null) { 
    System.out 
    .println("entity " + slurp(entity.getContent(), 10000000)); 
    System.out.println("entity " 
    + response.getStatusLine().getStatusCode()); 
    return true; 
} 
return false; 
} 


public static String slurp(final InputStream is, final int bufferSize) 
{ 
    final char[] buffer = new char[bufferSize]; 
    final StringBuilder out = new StringBuilder(); 
    try { 
    final Reader in = new InputStreamReader(is, "UTF-8"); 
    try { 
     for (;;) { 
     int rsz = in.read(buffer, 0, buffer.length); 
     if (rsz < 0) 
      break; 
     out.append(buffer, 0, rsz); 
     } 
    } 
    finally { 
     in.close(); 
    } 
    } 
    catch (UnsupportedEncodingException ex) { 
    /* ... */ 
    } 
    catch (IOException ex) { 
     /* ... */ 
    } 
    return out.toString(); 
} 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
String username = "number"; 
String password = "password"; 
String toMobile = "number"; 

String toMessage = "Message Sent"; 

MobileTry fullOnSMS = new MobileTry(username, password); 
try{ 
    if(fullOnSMS.isLoggedIn() && fullOnSMS.sendSMS(toMobile,toMessage)) 
    { 
    fullOnSMS.logoutSMS(); 
    System.out.println("Message was sent successfully "); 
    } 
    System.exit(0); 
} 

catch(IOException e) 
{ 
    System.out.println(e); 
    System.out.println("Unable to send message, possible cause: " + e.getMessage()); 
} 
} 
} 

以下是

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 
    at org.apache.http.impl.client.CloseableHttpClient.<init>(CloseableHttpClient.java:60) 
    at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:271) 
    at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146) 
    at sms.MobileTry.<init>(MobileTry.java:35) 
    at sms.MobileTry.main(MobileTry.java:142) 
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory 
    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) 
    ... 5 more 

回答

1

這是由commons-logging-xxx.jar缺失造成的錯誤。請將它添加到你的類路徑中。如果你使用Maven,那麼,

<dependency> 
    <groupId>commons-logging</groupId> 
    <artifactId>commons-logging</artifactId> 
    <version>1.1.1</version> 
</dependency> 

將依賴項添加到您的項目。希望這可以幫助!

+0

非常感謝,它解決了我的問題,+ 1爲相同 – SpringLearner

+0

歡迎先生:)感謝您upvote – Keerthivasan

+0

你值得投票,所以我給了並aceeptind你的答案too.Can你可以告訴我這個解決方案http:// stackoverflow.com/questions/19487628/exception-in-thread-main-java-lang-noclassdeffounderror-org-codehaus-jackson – SpringLearner