在android api23以上,就不推荐使用 httpclient 了。Google已经移除了Apache HttpClient的相关类。谷歌推荐使用:HttpUrlConnection 和 okhttp;
要想继续使用的话,只需要找到类库就行了。
查看了网上的解决方案:得到如下结果
解决方案一:
在android studio 中。找到 相应模块下的 build.gradle。
然后:注意!注意!注意!在android 配置项里配置:
android {
useLibrary ‘org.apache.http.legacy’
}
再然后:一定要 Sync 一下。尤其是网络慢的,要等他下载完全以后,再使用。
经过本人亲自测试,发现:在api33版本上 这个方案并不可行。
解决方案二:使用httpclient5,
相应模块下的 build.gradle,
然后:dependencies 配置项内添加:
dependencies {
api 'com.github.ok2c.hc5.android:httpclient-android:0.2.0'
}
再然后:一定要 Sync 一下。尤其是网络慢的,要等他下载完全以后,再使用。
但是已经是httpclient5 了,如果你手头上有原来的httpclent 代码,恭喜你!不能用
解决方案三:使用OKhttp:
如果要想使用 okhttp,只需要在 dependencies 配置项 内添加:
dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.4.2'
}
同样:使用的是OKhttp,手头上有原来的httpclent 代码, 不能用。
改变了gradle 配置以后:一定要 Sync 一下。尤其是网络慢的,要等他下载完全以后,再使用。
最后:如果你使用 http协议访问网络,而不是https,
则:必须在 AndroidManifest.xml 文件中 的<application> 的属性中加入:
android:usesCleartextTraffic="true"
例如:
<application .... android:usesCleartextTraffic="true" tools:targetApi="31"> .... </application>
如果你还想使用原有代码,
使用原来的Httpclient,那就将httpclient 类库 下载到本地,加入引用中: 如何用本地类库加入到项目总,请 参看我另外一篇文章Android Studio 调用 类库
类库下载地址:原httpclient 类库