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.