场景
升级华为升级android 9 ,发现接口调用无法成功
问题原因
应用官方的说明:在 Android 6.0 中,我们取消了对 Apache HTTP 客户端的支持。
从 Android 9 开始,默认情况下该内容库已从 bootclasspath 中移除且不可用于应用。且Android P 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉。
解决办法
首先手动引入org.apache.http.legacy.jar包置项目。并在Manifest文件中添加<uses-library android:name="org.apache.http.legacy" android:required="false"/> 。
Oktttp解决
res目录下新建xml目录,有点类似当年谷歌升级文件访问权限,在新建network_security_config.xml文件,内容如下
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
manifest文件<application>头引用 刚才新建的xml文件
<application
android:name=".MainApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/AppTheme">
这样就可以正常使用okhttp的网络请求了。
参考:
https://developer.android.google.cn/about/versions/pie/android-9.0-changes-28