java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpContext: method <init>()V not found

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1617042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:52:13  来源:igfitidea点击:

java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpContext: method <init>()V not found

javahttpclient

提问by Ravi Gupta

Hi I am trying to put up a small webapp but I am getting above error. Below is my code

嗨,我正在尝试建立一个小型 web 应用程序,但我遇到了上述错误。下面是我的代码

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("SomeURL"); // Using a URL local to my machine
// after setting nameValuePair and setting it on httppost
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

// This is where I am getting the above mentioned exception
HttpResponse response = httpclient.execute(httppost);

I am using httpclient-4.0-beta2.jar and httpcore-4.0.1.jar. It looks like BasicHttpContext is getting conflicted with some other jars in my app, but I couldn't figure it out. Any clue will be appreciated.

我正在使用 httpclient-4.0-beta2.jar 和 httpcore-4.0.1.jar。看起来 BasicHttpContext 与我的应用程序中的其他一些 jar 冲突,但我无法弄清楚。任何线索将不胜感激。

采纳答案by Malaxeur

It looks like you have a jar file with an old/newer version of BasicHttpContext. If there was a direct conflict, you'd receive a ClassNotFoundException. ClassLoaders are typically jerks about this kind of thing. In this case, the class exists however does not have the method that another library (I believe it's httpclient that's invoking the Context) was compiled against.

看起来你有一个旧/新版本的 jar 文件BasicHttpContext。如果存在直接冲突,您会收到一个ClassNotFoundException. 类加载器通常对这种事情很笨。在这种情况下,该类存在但是没有Context编译另一个库(我相信它是调用 的 httpclient )所针对的方法。