flutter 报错 DioError [DioErrorType.DEFAULT]: Bad state: Insecure HTTP is not allowed by platform

flutter 报错 DioError [DioErrorType.DEFAULT]: Bad state: Insecure HTTP is not allowed by platform

错误解释

平台不支持不安全的 HTTP 协议,即不允许访问 HTTP 域名的地址。

产生原因

IOS 和 Android 9.0 对网络请求做了一些限制,不能直接访问 Http 域名的地址。

解决方案

在安卓/android/app/src/main/AndroidManifest.xml中,添加
android:networkSecurityConfig="@xml/network_security_config"

 

pic
 
 
 
 
network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

or

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>

<!-- 配置7.0抓包--start -->
<debug-overrides>
    <trust-anchors>
        <!-- Trust user added CAs while debuggable only -->
        <certificates src="user"/>
    </trust-anchors>
</debug-overrides>
<!-- 配置7.0抓包--end -->

<!-- 配置9.0明文请求--start -->
<base-config cleartextTrafficPermitted="true" />
 <!-- 配置9.0明文请求--end -->   

</network-security-config>

 

 

 
项目文件夹建一个子文件夹res/xml,把network_security_config.xml保存到res/xml下
 

 重新编译!

 

 

posted @ 2020-10-31 22:55  anobscureretreat  阅读(1998)  评论(0编辑  收藏  举报