Android Q手机Retrofit请求报错

由于 Android P 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉。如果当前应用的请求是 http 请求,而非 https ,这样就会导系统禁止当前应用进行该请求

解决方法有三种:
(1)第一种方法:targetSdkVersion 降回到 27
(2)第二种方法:在 res 下新建一个 xml 目录,然后创建一个名为:network_security_config.xml 文件 ,该文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
然后在 AndroidManifest.xml application 标签内应用上面的xml配置:

    android:name=".App"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:networkSecurityConfig="@xml/network_security_config"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:theme="@style/AppTheme"></application>

(3)第三种方法:服务器和本地应用都改用 https (推荐)

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

友情链接更多精彩内容