我是Android Studio的新手,并且正在构建我的第一个专业应用程序,该应用程序将使用冰箱中的食材来建议食谱。我遇到了一个名为Spoonacular的API,该API表示代码使用了Unirest。因此,我加入了Internet权限,将unirest-java-1.4.9.jar文件放入libs中,并更新了我的依赖项。
apply plugin: 'com.android.application'
android {
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'
}
compileSdkVersion 28
defaultConfig {
applicationId "gupta.ansh.healthyref"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-
layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'org.apache.httpcomponents:httpclient:4.5.6'
implementation 'org.apache.httpcomponents:httpasyncclient:4.0.2'
implementation 'org.apache.httpcomponents:httpmime:4.5.2'
implementation 'org.json:json:20140107'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
但是Android Studio的IDE在依赖关系中的implementation 'org.apache.httpcomponents:httpclient:4.5.6'
和implementation 'org.json:json:20140107'
上显示错误。
当我将鼠标悬停在代码上时,它说:
httpclient defines classes that conflict with classes now provided by
Android. Solutions include finding newer versions or alternative
libraries that don't have the same problem (for example, for
httpclient use HttpUrlConnection or okhttp instead), or repackaging
the library using something like jarjar.
即使显示错误,该项目最初仍可正常运行。但是当我在项目中使用unirest时,
HttpResponse<JsonNode> response = Unirest.post("https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/food/products/classify")
.header("X-Mashape-Key", "my_private_key")
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.body("{\"title\":\"banana\",\"upc\":\"\",\"plu_code\":\"\"}")
.asJson();
我调试了它,它给了我InvocationTargetException
并崩溃了。
是的,我知道Spoonacular有一个Android SDK,但是它太旧了。它说最新的版本在Mashape上,但是我在那儿找不到它,并且从论坛上判断,也没有其他人可以找到。
那么如何解决此InvocationTargetException问题?还是没有办法解决,我必须放弃Unirest?