ionic4真机 android9.0 http请求失败问题

问题:

由于跨域问题,在浏览器调试中,使用代理可以向后台发送请求;
在真机调试中不存在跨域,所以把代理去掉,但正常的http请求却过不去。

原因:

android9请求要为https的。

解决办法:

打开android的manifest.xml文件,在application标签中添加 android:networkSecurityConfig="@xml/network_security_config":
image.png
然后在platforms\android\app\src\main\res\xml\network_security_config.xml文件中增加代码:


    

image.png

补充:

由于platform有时会remove掉重新add,所以每次都要去重新配network_security_config.xml就显得特别麻烦,后来我在ionic start myApp blank时发现ionic默认在myApp/config.xml里已经把此配置提出来了,看:


        
            
        
        
        
        
        
        ...

可以看到 用myApp/resources/android/xml/network_security_config.xml覆盖platforms/android/app/src/main/res/xml/network_security_config.xml,所以只要把myApp/resources/android/xml/network_security_config.xml此文件修改一次之后就可,即使你的platform移除重新添加后,也不用再重新改了。

你可能感兴趣的:(ionic4真机 android9.0 http请求失败问题)