I’m working on an Android app and trying to connect to a food recipe API using Unirest library. I followed the setup instructions for manual jar installation since I’m not using Maven.
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 am I missing in my setup? Any ideas on how to resolve this issue would be really helpful. Thanks!
looks like ur missing the async http components that unirest needs internally. try adding implementation 'org.apache.httpcomponents:httpcore:4.3.3' and implementation 'org.apache.httpcomponents:httpasyncclient:4.0.2' to ur gradle. the DefaultConnectingIOReactor is part of async client stuff even if ur not explicitly using async calls.
The NoClassDefFoundError you are experiencing is related to Unirest’s internal dependency on Apache HttpComponents’ asynchronous client libraries. Even though your code appears to be making synchronous calls, Unirest initializes its async capabilities during startup. I faced a similar situation when integrating payment gateway APIs in my e-commerce app. The solution involves adding the missing httpasyncclient dependency which contains the DefaultConnectingIOReactor class. Update your gradle dependencies to include implementation 'org.apache.httpcomponents:httpasyncclient:4.0.2' along with the corresponding httpcore-nio version. However, based on my experience with Android development, I would recommend reconsidering your choice of HTTP library. Unirest can be overkill for mobile applications and may increase your APK size significantly. Libraries like Volley or Retrofit are specifically designed for Android and handle network operations more efficiently on mobile devices.
This error occurs because Unirest requires the httpcore-nio library for asynchronous operations, which isn’t included in your dependencies. The DefaultConnectingIOReactor class is part of the Apache HttpCore NIO package that handles non-blocking I/O operations. Add this dependency to your gradle file: implementation 'org.apache.httpcomponents:httpcore-nio:4.3.3'. I encountered the same issue when integrating a weather API in my Android project last year. After adding httpcore-nio, the NoClassDefFoundError disappeared completely. Make sure the version matches your other Apache HTTP components to avoid compatibility issues. You might also want to consider using OkHttp or Retrofit instead of Unirest for Android development, as they’re more optimized for mobile applications and have better Android support.