我收到java.lang.NoClassDefFoundError:org / apache / http / conn / params / ConnPerRoute
当我尝试使用Spring,Android REST框架时,会发生此错误。代码非常简单,如下所示。
在我看来,这个问题应该是一个依赖问题,但我已经包含了所需的Spring依赖项和org.apache.httpcomponents',' httpclient-android'依赖问题一直存在。
我对如何进行感到茫然。下面是问题代码的片段,以及我的build.gradle文件和完整的异常打印输出。
任何人都可以帮我解决这个问题吗?非常感谢。
错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/params/ConnPerRoute
at org.springframework.http.client.support.HttpAccessor.<init>(HttpAccessor.java:71)
at org.springframework.http.client.support.InterceptingHttpAccessor.<init>(InterceptingHttpAccessor.java:36)
at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:156)
at com.lazeebear.ServerConnector.ServerConnector.signin(ServerConnector.java:27)
at com.lazeebear.main.main(main.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.params.ConnPerRoute
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more
代码: public static boolean signin(String email,String password){
// Define endpoint
String url = baseURL + signinEndpoint;
// Create parameters for POST request
String parameters = "?email=" + email + "&password=" + password;
// Make POST request
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Object> entity = restTemplate.postForEntity(url, getHeaders(), Object.class, parameters);
// Get and set cookies
List<String> cookies = entity.getHeaders().get(COOKIES_HEADER);
setCookies(cookies);
HttpStatus status = entity.getStatusCode();
if (status.equals(HttpStatus.OK)) {
return true;
}
else {
return false;
}
}
的build.gradle: 申请插件:&#39; com.android.application&#39;
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "com.lazeebear"
minSdkVersion 19
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
// Dependencies for payment
compile 'com.braintreepayments.api:braintree:2.+'
compile 'com.braintreepayments.api:drop-in:2.+'
// Dependencies for Spring
compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
// Dependencies for GSON
compile group: 'com.google.code.gson', name: 'gson', version: '2.4'
// Test
compile group: 'org.apache.httpcomponents', name: 'httpclient-android', version: '4.3.3'
}
答案 0 :(得分:0)
如果查看httpclient-android JAR的内容,那么该类不存在。
然而,该类确实存在于:
org.apache.httpcomponents http-client JAR。
http://www.findjar.com/class/org/apache/http/conn/params/ConnPerRoute.html