java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27263441/
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-11-02 11:30:48  来源:igfitidea点击:

java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager

javaandroidclasspath

提问by takeradi

I am using an API from Mashape in an Android application. Mashape recommends using the Unirest library. I have included the dependencies mentioned on the unirest link: http://unirest.io/java

我在 Android 应用程序中使用 Mashape 的 API。Mashape 推荐使用 Unirest 库。我已经包含了 unirest 链接中提到的依赖项:http://unirest.io/java

My Dependencies:

我的依赖:

compile fileTree(dir: 'libs', include: ['*.jar'])
compile group:'com.mashape.unirest', name:'unirest-java', version:'1.3.26'
compile group:'org.apache.httpcomponents', name:'httpcore', version:'4.3.3'
compile group:'org.apache.httpcomponents', name:'httpclient', version:'4.3.6'
compile group:'org.apache.httpcomponents', name:'httpasyncclient', version:'4.0.2'
compile group:'org.apache.httpcomponents', name:'httpmime', version:'4.3.5'
compile group:'org.json', name:'json', version:'20141113'
compile 'com.android.support:appcompat-v7:21.0.+'

I still get this error when I make a HTTPRequest:

当我发出 HTTPRequest 时,我仍然收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager
    at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:153)
    at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:133)
    at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
Disconnected from the target VM, address: '127.0.0.1:58805', transport: 'socket'

Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.HttpClientConnectionManager
    at java.net.URLClassLoader.run(URLClassLoader.java:372)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more

My java code is simple:

我的java代码很简单:

HttpResponse<JsonNode> response = Unirest.get("https://testlink/all")
                    .header("X-Mashape-Key", TEST_KEY)
                    .asJson();

Could you please help me solve this issue? Thanks for your help!

你能帮我解决这个问题吗?谢谢你的帮助!



UPDATE:As per secondo's suggestion I changed the httpclient to httpclient-android. My dependencies look like this now:

更新:根据 secondo 的建议,我将 httpclient 更改为 httpclient-android。我的依赖项现在看起来像这样:

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile group:'com.mashape.unirest', name:'unirest-java', version:'1.3.26'
    compile group:'org.apache.httpcomponents', name:'httpclient-android', version:'4.3.5'
    compile group:'org.apache.httpcomponents', name:'httpcore', version:'4.3.3'
    compile group:'org.apache.httpcomponents', name:'httpcore-nio', version:'4.3.3'
    compile group:'org.apache.httpcomponents', name:'httpasyncclient', version:'4.0.2'
    compile group:'org.apache.httpcomponents', name:'httpmime', version:'4.3.5'
    compile group:'org.json', name:'json', version:'20141113'
    compile 'com.android.support:appcompat-v7:21.0.0'

After rebuilding the project and running it, I get this error:

重建项目并运行后,我收到此错误:

Error:Gradle: Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    H:\IDEs\Android\android-sdk\sdk\build-tools.1.1\dx.bat --dex --output H:\workspace\app\build\intermediates\dex\debug H:\workspace\app\build\intermediates\classes\debug H:\workspace\app\build\intermediates\dependency-cache\debug H:\workspace\app\build\intermediates\pre-dexed\debug\classes-4bcf10ca50c0eb5fadc28458c4bdfc2547896050.jar H:\workspace\app\build\intermediates\pre-dexed\debug\classes-5f3630f6ee4db4f375d08625dd3729b85011cca4.jar H:\workspace\app\build\intermediates\pre-dexed\debug\httpasyncclient-4.0.2-5690889a573efdb127c3d5c2454e7a393211dee6.jar H:\workspace\app\build\intermediates\pre-dexed\debug\httpclient-android-4.3.5-1f1bd070e63b095d84e38ae09b61037bfca0f0ab.jar H:\workspace\app\build\intermediates\pre-dexed\debug\httpcore-4.3.3-ee2fa72794df9164a47403637dad2f0f578b8086.jar H:\workspace\app\build\intermediates\pre-dexed\debug\httpcore-nio-4.3.3-a277a34ed4caa7515de7b22c51b9b5f43eb3a9ac.jar H:\workspace\app\build\intermediates\pre-dexed\debug\httpmime-4.3.5-ed96b5f192c9ed4e3cca5097f8fe8a3d2a673970.jar H:\workspace\app\build\intermediates\pre-dexed\debug\internal_impl-21.0.0-529c245f53e03251f5328b23447088bd0d76c596.jar H:\workspace\app\build\intermediates\pre-dexed\debug\support-annotations-21.0.0-bc18e9201600d41ee5718ce129ef80828a7bb5d5.jar H:\workspace\app\build\intermediates\pre-dexed\debug\unirest-java-1.3.26-7ee6377b956e1b237982bd82965679bf6e0a7fa9.jar
Error Code:
    2
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lorg/apache/http/Consts;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:302)
        at com.android.dx.command.dexer.Main.run(Main.java:245)
        at com.android.dx.command.dexer.Main.main(Main.java:214)
        at com.android.dx.command.Main.main(Main.java:106)

回答by Altmish-E-Azam

there is no class file in your project for following classe.

您的项目中没有用于以下类的类文件。

org/apache/http/conn/HttpClientConnectionManager

So please add jar file in your project library you can downalod this jar file from below link

所以请在你的项目库中添加 jar 文件,你可以从下面的链接下载这个 jar 文件

Download Jar File

下载 Jar 文件

回答by Siddharth_Vyas

Go to Project/Properties/Java Build Path/Order and Export -- Make sure there's a check in front of Android Dependencies and the support library, if you use it.

转到 Project/Properties/Java Build Path/Order and Export -- 确保在 Android Dependencies 和支持库前面有一个检查,如果你使用它。

Mark all checkboxes and Click on Apply and clean the project.

标记所有复选框并单击应用并清理项目。