问题
Using the code I found here: http://hc.apache.org/httpcomponents-client-ga/quickstart.html I created a quick HTTP client application. Here is a code snippet:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
log.info("Logging in");
HttpResponse response;
try {
response = httpclient.execute(httpGet);
}
catch (ClientProtocolException cpe) {
String msg = "Error logging in with:" + rootUrl;
log.error(msg, cpe);
throw new BusinessRuleException(msg, cpe);
}
catch (IOException ioe) {
String msg = "Error logging in with:" + rootUrl;
log.error(msg, ioe);
throw new BusinessRuleException(msg, ioe);
}
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity);
}
catch (IOException ioe) {
String msg = "Error retrieving login response:" + rootUrl;
log.error(msg, ioe);
throw new BusinessRuleException(msg, ioe);
}
finally {
httpGet.releaseConnection();
}
I get this exception:
java.lang.NoSuchMethodError: org.apache.http.client.methods.HttpGet.releaseConnection()V
at com.xerox.tclg.juror.schedule.RestScheduleRunner.runDailyCheckout(RestScheduleRunner.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:276)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:260)
I found some docs that said releaseConnection() wasn't necessary, so I deleted the finally block. Then I get this exception:
java.lang.NoSuchMethodError: org.apache.http.util.EntityUtils.consume(Lorg/apache/http/HttpEntity;)V
at com.xerox.tclg.juror.schedule.RestScheduleRunner.runDailyCheckout(RestScheduleRunner.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:276)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:260)
Has anyone seen this?
回答1:
In this case, the java.lang.NoSuchMethodError
most likely means that you have got the wrong version of one of the Http Components JAR files.
It would probably help you diagnose the problem if you could find out exactly what is happening at "RestScheduleRunner.java:85".
来源:https://stackoverflow.com/questions/12839473/httpcomponents-not-working-as-documented