gpt4 book ai didi

java - 在 Android Studio 中使用 unirest 的问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:44:27 32 4
gpt4 key购买 nike

我的 logcat 中出现了一些错误,这些错误导致我的应用程序在尝试执行一些 unirest 请求时崩溃。我知道您必须生成一个包含所有依赖项的特殊 unirest jar,但我在使用 maven 时遇到问题并且没有这样做,而是将依赖项单独包含在 libs 文件夹中。我已将问题缩小到下面的这个错误。

日志:

java.lang.VerifyError: com/mashape/unirest/http/options/Options
at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:154)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:131)
at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)

我知道 java.lang.verifyerror 是由编译时和运行时给出的不同库引起的。但我不知道如何解决这个问题。请帮忙,确保我需要的所有依赖项都在下面,因为我之前没有使用过 unirest。

构建. Gradle :

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/unirest-java-1.3.20.jar')
compile files('libs/rebound-0.3.6.jar')
compile files('libs/commons-io-2.4.jar')
compile files('libs/json-20140107.jar')
compile files('libs/httpmime-4.3.5.jar')
compile files('libs/httpclient-4.3.5.jar')
compile files('libs/httpasyncclient-4.0.2.jar')
compile files('libs/httpcore-4.3.2.jar')
compile files('libs/commons-codec-1.6.jar')
compile files('libs/commons-logging-1.1.3.jar')
}

我的 libs 文件夹是什么样子的:

commons-io-2.4.jar
httpcore-4.3.2.jar
httpclient-4.3.5.jar
commons-logging-1.1.3.jar
json-20140107.jar
rebound-0.3.6.jar
unirest-java-1.3.20.jar
commons-codec-1.6.jar
httpmime-4.3.5.jar
httpasyncclient-4.0.2.jar

还将此包含在 build.gradle 中,因为我在尝试构建 gradle 文件时遇到重复文件错误:

packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}

最佳答案

我知道你问这个问题已经有一段时间了。我认为 Android 会覆盖位于 org.apache.http 中的公共(public)类。你不能自己覆盖它们。尝试将下载的包重新定位到另一个位置,例如 surrogate.org.apache.http 以正确使用它们。

来自 https://hc.apache.org/httpcomponents-client-4.3.x/android-port.html :

Google Android 1.0 was released with a pre-BETA snapshot of Apache HttpClient. To coincide with the first Android release Apache HttpClient 4.0 APIs had to be frozen prematurely, while many of interfaces and internal structures were still not fully worked out. As Apache HttpClient 4.0 was maturing the project was expecting Google to incorporate the latest code improvements into their code tree. Unfortunately it did not happen. Version of Apache HttpClient shipped with Android has effectively become a fork. Eventually Google decided to discontinue further development of their fork while refusing to upgrade to the stock version of Apache HttpClient citing compatibility concerns as a reason for such decision. As a result those Android developers who would like to continue using Apache HttpClient APIs on Android cannot take advantage of newer features, performance improvements and bug fixes.

如果您继续阅读,您会发现以下内容:

Some of the implementation classes had to be copied (or shaded) with different names in order to avoid conflicts with the older versions of the same classes included in the Android runtime. One can increase compatibility of with the stock version of HttpClient by avoiding 'org.apache.http.**.*HC4' classes.

使用

dependencies {
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}

包含 http 组件。

您可能必须使用 shadowJar如果您不想手动重新定位它们,则可以重新定位您的依赖项的插件。

编辑:

我找到了 this很棒的教程,对我很有帮助。甚至认为我会推荐使用 AndroidAsync 或 Volley 来建立与网络服务器的连接。

关于java - 在 Android Studio 中使用 unirest 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26771975/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com