Problems using unirest in Android Studio

半腔热情 提交于 2019-11-28 10:30:45

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!