使用unirest在Android的工作室问题 [英] Problems using unirest in Android Studio

查看:697
本文介绍了使用unirest在Android的工作室问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到我的logcat中的一些错误,同时试图执行一些unirest请求崩溃我的应用程序。我知道你有产生特殊unirest罐的所有依赖关系,但我有问题与Maven,并没有这样做,而是我已经包括了单独依赖于libs文件夹。我已经缩小的问题,向下跌破这一单一的错误。

logcat的:

  java.lang.VerifyError:COM / mashape / unirest / HTTP /选项/选项
        在com.mashape.unirest.http.HttpClientHelper。prepareRequest(HttpClientHelper.java:154)
        在com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:131)
        在com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
 

据我所知,java.lang.verifyerror是由在编译时间与运行时间赋予了不同的库。但我不知道如何解决这个问题。请帮帮忙,确保所有我需要的相关性低于,因为我没有使用unirest在此之前。

Build.Gradle:

 相关性{
编译文件树(包括:['的* .jar'],DIR:库)
编译文件(库/ unirest-java的1.3.20.jar)
编译文件(库/反弹-0.3.6.jar)
编译文件(库/公共-IO-2.4.jar)
编译文件(库/ JSON-20140107.jar)
编译文件(库/ httpmime-4.3.5.jar)
编译文件(库/ HttpClient的-4.3.5.jar)
编译文件(库/ httpasyncclient-4.0.2.jar)
编译文件(库/的HttpCore-4.3.2.jar)
编译文件(库/ commons- codeC-1.6.jar)
编译文件(库/共享记录-1.1.3.jar)
}
 

什么我libs文件夹的样子:

 的commons-IO-2.4.jar
的HttpCore-4.3.2.jar
HttpClient的-4.3.5.jar
共享记录-1.1.3.jar
JSON-20140107.jar
反弹-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包括这一点,因为我得到了重复文件错误,而试图建立的摇篮文件:

  packagingOptions {
    不包括META-INF / DEPENDENCIES
    不包括META-INF /注意事项
    不包括META-INF /许可证
    不包括META-INF / LICENSE.TXT
    不包括META-INF / NOTICE.txt
}
 

解决方案

我知道已经有一段时间,因为你问这个问题。我认为Android将覆盖位于org.apache.http通用类。自己不能覆盖他们。尝试下载的软件包移居到其他位置,如surrogate.org.apache.http正确地使用它们。

https://hc.apache.org/httpcomponents -client-4.3.x版/ Android的port.html

  

谷歌Android 1.0发布与Apache的HttpClient的pre-BETA快照。到与第一机器人重合释放阿帕奇HttpClient的4.0的API必须pmaturely冻结$ P $,而许多的接口和内部结构进行了还没有完全解决。作为阿帕奇的HttpClient 4.0是成熟的项目,预计谷歌将最新code改进到他们的code树。遗憾的是它并没有发生。 Apache的HttpClient的版本附带的Andr​​oid实际上已经成了一个叉。最终,谷歌决定停止其叉的进一步发展,同时拒绝升级到股票版本的Apache HttpClient的理由是兼容性问题为理由,这样的决定。其结果是这些Android开发者谁愿意继续使用Apache的HttpClient的API在Android上不能充分利用新的功能,性能改进和bug修复。

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

  

一些实现类不得不被复制(或阴影)以不同的名称,以避免与旧版本的包括在Android运行相同的类冲突。人们可以通过避免增加与股票版本HttpClient的兼容性org.apache.http。**。* HC4类。

使用

 相关性{
    编译组:org.apache.httpcomponents,名称:HttpClient的,Android的版本:4.3.5.1
}
 

要包括httpcomponents。

您可能需要使用 shadowJar 插件重新定位你的依赖,如果你不想要重新定位这些手工操作。

编辑:

我发现的这工作真棒教程对我来说。甚至认为我会建议使用AndroidAsync或凌空建立连接到网络服务器。

I have been getting some errors in my logcat that crash my application while trying to execute some unirest requests. I know you have to generate a special unirest jar with all the dependencies but I have had problems with maven and did not do this, instead I have included the dependencies separate in the libs folder. I have narrowed the problem down to this single error below.

Logcat:

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)

I understand that the java.lang.verifyerror is caused by a different library given at compile time vs run time. But I don't know how to fix this. Please help, make sure all the dependencies I need is below since I have not used unirest before this.

Build.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')
}

what my libs folder looks like:

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

also included this in the build.gradle because I was getting a duplicate file error while trying to build the gradle file:

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

解决方案

I know it has been a while since you asked that question. I think Android overwrites common classes located in org.apache.http. You can not overwrite them by yourself. Try to relocate the downloaded packages to another location such as surrogate.org.apache.http to use them properly.

From 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.

If you continue reading you will find the following:

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.

Use

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

to include the httpcomponents.

You may have to use the shadowJar plugin to relocate your dependencies if you don't want to relocate them manually.

Edit:

I found this awesome tutorial which worked out for me. Even thought I would recommend to use AndroidAsync or Volley to build up connections to a webserver.

这篇关于使用unirest在Android的工作室问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆