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?