1、問題緣由:android
根本緣由是從Android9.0開始,出於徹底因素考慮,默認再也不支持http網絡請求,須要使用 https。網絡
2、解決方案:app
解決的基本思路是:對指定的網址進行過濾,強制容許指定網址繼續使用http請求dom
參考地址1:https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permittedide
參考地址2:https://developer.android.com/training/articles/security-config#CleartextTrafficPermittedui
參考地址3: https://koz.io/android-m-and-the-war-on-cleartext-traffic/this
下列解決步驟基於參考地址1整理:spa
方案 1: rest
A:建立 res/xml/network_security_config.xmlcode
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">Your URL(ex: 127.0.0.1)</domain> </domain-config> </network-security-config>
注意:上述代碼的意思是解除對指定網址的限制。
B:修改 AndroidManifest.xml
配置 android:networkSecurityConfig
<?xml version="1.0" encoding="utf-8"?> <manifest ...> <uses-permission android:name="android.permission.INTERNET" /> <application ... android:networkSecurityConfig="@xml/network_security_config" ...> ... </application> </manifest>
方案 2:
A:修改AndroidManifest.xml
啓用 android:usesCleartextTraffic
<?xml version="1.0" encoding="utf-8"?> <manifest ...> <uses-permission android:name="android.permission.INTERNET" /> <application ... android:usesCleartextTraffic="true" ...> ... </application> </manifest>
方案 3:
在 @david.s'的回答中指出 也可能會致使該問題 -
在 Manifest Docs https://developer.android.com/guide/topics/manifest/manifest-element#targetSandboxVersion中對於 有以下描述
android:targetSandboxVersion
The target sandbox for this app to use. The higher the sandbox version number, the higher the level of security. Its default value is 1; you can also set it to 2. Setting this attribute to 2 switches the app to a different SELinux sandbox. The following restrictions apply to a level 2 sandbox:
The default value of usesCleartextTraffic in the Network Security Config is false.Uid sharing is not permitted.
因此,若是你在 AndroidManifest.xml 的節點中配置了 ,須要將它的值置爲1.
A:修改AndroidManifest.xml
下降 android:targetSandboxVersion 的版本
<?xml version="1.0" encoding="utf-8"?> <manifest android:targetSandboxVersion="1"> <uses-permission android:name="android.permission.INTERNET" /> ... </manifest>