Android Unirest Integration Error: NoClassDefFoundError for DefaultConnectingIOReactor Class

I’m working on an Android project where I need to connect to a food recipe API using the Unirest library. I followed the setup instructions for manual jar integration, but I keep encountering a class resolution error.

Here’s my Gradle configuration:

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'
}

My API call code looks like this:

HttpResponse<JsonNode> apiResult = null;
try {
    apiResult = Unirest.get("https://food-api-endpoint.com/search?query=chicken%2Crice&count=10&sort=1")
            .header("X-API-Key", "my_secret_key")
            .header("X-API-Host", "food-api-endpoint.com")
            .asJson();
} catch (UnirestException ex) {
    ex.printStackTrace();
}

The app crashes with this error message:

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)

What might I be missing in my setup? Any tips would be greatly appreciated.

drop unirest for android - it’s meant for servers and you’ll keep hitting nio problems. i’ve been there, not worth it. use retrofit instead, or just stick with httpurlconnection if you want simple. those dependency conflicts in your gradle are already telling you something’s wrong.

Had the exact same problem with Unirest on Android. The issue is Unirest’s built for servers and uses NIO reactor classes that don’t exist in Android’s stripped-down Apache HTTP setup. You might get the missing dependencies working, but you’ll just hit more walls later. I ditched it for Volley in my recipe app - way better experience. Different syntax but easy to pick up. You could also try Retrofit with Gson converter - it handles JSON parsing automatically and the setup’s cleaner. Both are Android-native so you won’t deal with these classpath headaches from cramming server libraries into mobile.

DefaultConnectingIOReactor is part of Apache HttpComponents NIO modules, which aren’t in your dependencies. Unirest uses Apache HttpAsyncClient for async operations, but you’ve only got the regular httpclient-android dependency. You’d need to add HttpAsyncClient to fix this, but here’s the problem - HttpAsyncClient doesn’t play nice with Android because of how Android handles NIO operations. Don’t bother fighting these compatibility issues. Switch to OkHttp instead. It’s lightweight, built for Android, and handles JSON responses without all these dependency headaches. Since you’re just doing a simple GET request with headers, migration won’t be painful. You’ll save yourself hours of debugging classpath problems that always pop up when using server libraries like Unirest on Android.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.