• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Commons to access URL

 
Stephen Hanley
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone feeling generous and like to read the below?

It's throwing a "java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException" error?

/**
* @ejb.interface-method
* view-type="remote"
*
*/
public String sendMessage(String sender, String recipient, String text){
HttpClient client = new HttpClient();
String url = "http://myapi.com/http/sendmsg";
String username = "aaa";
String password = "bbb";
String api_id = "ccc";
String status = "";
String responseBody = "";
PostMethod method = new PostMethod(url);
NameValuePair[] data = {
new NameValuePair("user",username),
new NameValuePair("password",password),
new NameValuePair("api_id",api_id),
new NameValuePair("from",sender),
new NameValuePair("to",recipient),
new NameValuePair("text",text)
};

method.addParameters(data);

DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
retryhandler.setRequestSentRetryEnabled(false);
retryhandler.setRetryCount(3);
method.setMethodRetryHandler(retryhandler);

try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}

// Read the response body.
responseBody = method.getResponseBodyAsString();

// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
//status = new String(responseBody);

} catch (IOException e) {
System.err.println("Failed to download file.");
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
return responseBody;
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure org/apache/commons/codec/DecoderException is in your classpath.
 
Stephen Hanley
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nearly two weeks this has been wrecking the proverbial head.

Finally working perfectly. Thanks all at Javaranch for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!