0
votes

I am receiving java.lang.NoClassDefFoundError: org/apache/http/conn/params/ConnPerRoute

This error is occurring as I attempt to use the Spring, Android REST framework. The code is fairly simple and is given below.

It seems to me that this issue should be a dependency issue, but I have included both the required Spring dependencies and org.apache.httpcomponents', 'httpclient-android' dependency the issue has persisted.

I am at a loss on how to proceed. A snippet of the problem code is below, along with my build.gradle file, and the full exception printout.

Can anyone help me resolve this issue? Many thanks.

Error:

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

Code: 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: apply plugin: 'com.android.application'

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'
}
1
Android has remove apache HTTP lib from android.jar from API 24 and you are targeting the same - dex
so you need to use legacy code in your gradle, Android has pretty good documentation for the same. Otherwise you could lower down your target SDK to 23 but you will not leverage the capabilities of M then - dex

1 Answers

0
votes

If you look at the contents of the httpclient-android JAR then that class does not exist.

That class does however exist in:

org.apache.httpcomponents http-client JAR.

http://www.findjar.com/class/org/apache/http/conn/params/ConnPerRoute.html