NoClassDefFoundError for DefaultConnectingIOReactor while setting up Unirest for food API integration

I’m working on an Android app that needs to connect to a food recipe API using Unirest library. When I try to make the API call, I keep getting a NoClassDefFoundError about DefaultConnectingIOReactor.

Here’s my gradle setup:

apply plugin: 'com.android.application'

android {
    useLibrary 'org.apache.http.legacy'
    
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/ASL2.0'
    }
}

dependencies {
    implementation 'com.rapidapi:rapidconnect-android:0.1'
    implementation files('libs/unirest-java-1.4.9.jar')
    implementation 'org.json:json:20171018'
    implementation('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
    }
    implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'
}

And here’s the code that triggers the error:

HttpResponse<JsonNode> apiResponse = null;
try {
    apiResponse = Unirest.get("https://food-api-v1.p.mashape.com/recipes/searchByItems?items=chicken%2Crice&limit=10&sort=1")
            .header("X-Mashape-Key", "my-api-key")
            .header("X-Mashape-Host", "food-api-v1.p.mashape.com")
            .asJson();
} catch (UnirestException ex) {
    ex.printStackTrace();
}

The error shows:

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/impl/nio/reactor/DefaultConnectingIOReactor;
    at com.mashape.unirest.http.options.Options.refresh(Options.java:85)
    at com.mashape.unirest.http.options.Options.<clinit>(Options.java:46)

I’ve tried adding the Apache HTTP legacy library but still getting this issue. Has anyone faced this problem before? What am I missing in my configuration?

You’re mixing HTTP client libraries on Android - that’s your problem. Unirest wasn’t built for Android and its dependencies clash with Android’s networking stack. Been there, spent hours debugging the same mess when integrating a third-party API. Don’t waste time making Unirest work. Switch to OkHttp or Retrofit instead - they’re made for Android and handle API integration way cleaner. No more Apache HTTP version conflicts to deal with. If you’re dead set on Unirest, grab the standalone JAR with bundled dependencies, but honestly just use Android’s native HTTP clients. You’ll save yourself major headaches.

Had this exact issue with a recipe API last year. Android strips out Apache HTTP NIO classes that Unirest needs, so adding httpcore-nio doesn’t really solve it - you’re still fighting Android’s HTTP limitations. I downgraded to unirest-java-1.3.11 instead of 1.4.9 since the older version doesn’t use those problematic NIO reactor classes. Also ditch the httpclient-android dependency - it conflicts with Unirest’s standard httpclient. Keep your legacy library declaration though. This got my food API calls working without having to switch libraries completely.

u def need the nio reactor dependency. just add implementation 'org.apache.httpcomponents:httpcore-nio:4.3.2' in gradle - that should fix the NoClassDefFoundError for DefaultConnectingIOReactor.