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下
 

 重新编译!

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM