Didn't-find-class-"org-apache-http-entity-StringEntity"-on-path

今天在测试android api 28上的兼容问题时,出现2个的异常:

Didn't find class "org.apache.http.entity.StringEntity" on path

在过去的 Android 6 中,取消了对 Apache HTTP 客户端的支持, 而从 Android 9 开始官方又过分了,默认情况下该内容库已从 bootclasspath 中移除且不可用于应用,原因未知,望告知。
所以 Android 6.0 以上时,如果想使用org.apache.http的话,可以在gradle中android配置下写入:

useLibrary 'org.apache.http.legacy'

在Android 9以上时,如果还要继续使用org.apache.http的话,可以在AndroidManifest.xml中application节点下面写入:

<uses-library
    android:name="org.apache.http.legacy"
    android:required="false" />

而第二个异常:

onFailurejava.io.IOException: Cleartext traffic not permitted

其原因是在Android 9以上系统中,默认情况下启用网络传输层安全协议 (TLS),是不支持HTTP明文请求的,也就是支持HTTPS了。
所以解决方法是:
1.改成https请求
2.修改targetSdkVersion为28以下
3.向别人学习更全面的解决方案

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容