安卓开发,okhttp直接onFailure的原因之一,被禁用的http

Android P(API28)全面禁止了非https链接,并严格审查网站的CA证书

1. 方法一

网上比较多的方面是采用创建xml文件然后在清单中的application添加属性。
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>

然后在清单中

<application
        ...
        android:networkSecurityConfig="@xml/network_security_config">

2. 方法二(更简单)

不需要创建xml文件直接在清单的application中,总体来说还是简单写(有人加了第二句忽略后失败,所以如果不成功,试试删掉tools:ignore这句)

<application
        ...
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning"
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐