解决HttpClient在Android5.0系统上无效的问题

坤乾_老毛 2016-01-28 11:06:51
以前的框架用到HttpClient,但是由于在Android5.0里面HttpClient已经无效,但有不想换框架,所以找到了这种解决方法,该方式是在在Studio中配置。
1:在build.gradle里面添加配置
useLibrary 'org.apache.http.legacy'
例如我的配置:
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "xxx.xxx.xxx"
minSdkVersion 14
targetSdkVersion 17
versionCode 1
versionName "1.0"
}
}
2:编译的时候会报错,所以需要在sdk里面添加配置文件。
我在在build.gradle配置的编译sdk版本 是android-20,所以找到sdk/platforms/android-20目录。
在目录下新建optional(如果已经存在,则不需要新建)。
在目录下新建optional.json文本文件,里面的内容如下:
[
{
"name":"org.apache.http.legacy",
"jar":"org.apache.http.legacy.jar",
"manifest":false
}
]
同时把org.apache.http.legacy.jar复制到该目录下。
org.apache.http.legacy.jar和json问本的下载地址如下:
http://download.csdn.net/detail/maozonggen/9420652

3:clean你的项目,重新编译运行
...全文
401 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
坤乾_老毛 2016-02-02
  • 打赏
  • 举报
回复
记录一下解决方法,不要在意这些细节
JarQ 2016-01-29
  • 打赏
  • 举报
回复
话说不是在6.0才不能用吗?在5.0是可以用的.而且google已经告诉我们怎么使用,目前来说此库的api并非移除了,只是隐藏了,开发者正常情况下是无法调用的,官方给出的方案是 Apache HTTP Client Removal Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption. To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:
android {
    useLibrary 'org.apache.http.legacy'
}
官方change log link<请自行FQ>:http://developer.android.com/intl/zh-cn/about/versions/marshmallow/android-6.0-changes.html

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧