我正在尝试使用Android从RapidApi实现Spoonacular Api。它使用Unirest,因此我正在尝试对其进行配置。在official documentation上,我关注Without Maven
部分。我下载了unirest jar,并将它们放在我的build.gradle
apply plugin: 'com.android.application'
android {
.......
useLibrary 'org.apache.http.legacy'
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
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'
}
apply plugin: 'com.google.gms.google-services'
这是我的Java代码:
HttpResponse<JsonNode> response = null;
try {
response = Unirest.get("https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients?ingredients=egg%2Cyogurt&number=5&ranking=1")
.header("X-Mashape-Key", "keyyy")
.header("X-Mashape-Host", "spoonacular-recipe-food-nutrition-v1.p.mashape.com")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
我收到此错误:
09-19 11:53:19.674 6789-6789/com.example.hassen.androidpim E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hassen.androidpim, PID: 6789
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)
at com.mashape.unirest.http.options.Options.getOption(Options.java:42)
您能帮我找出解决方法吗? 谢谢
答案 0 :(得分:1)
我相信您错过了 Apache依赖项之一(我想是httpasyncclient
)。
实际上the documentation提到的有其中的三个:
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
因此,请转到此链接下载-在项目内部手动添加jar文件:
http://hc.apache.org/downloads.cgi
然后查找:HttpAsyncClient
。
使用它在项目内部添加jar
文件库:How to add a jar in External Libraries in android studio