[問題]
使用 WebView 查看 http 網址,出現 net::ERR_CLEARTEXT_NOT_PERMITTED 錯誤。
[原因]
Android 9 (API level 28) 之後的 API,預設只允許 HTTPS 的加密網站,不允許 HTTP 的明文。
修改 AndroidManifest.xml,改成可使用明文(cleartext)傳輸,
加上 android:usesCleartextTraffic="true" 設定
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test"> <uses-permission android:name="android.permission.INTERNET" /> <application ..... android:usesCleartextTraffic="true" .....> ..... </application> </manifest>
參考:
- https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic
<application> | Android Developers - https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted
Android 8: Cleartext HTTP traffic not permitted - Stack Overflow - https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted
Network security configuration | Android Developers - https://android-developers.googleblog.com/2016/07/changes-to-trusted-certificate.html
Android Developers Blog: Changes to Trusted Certificate Authorities in Android Nougat
沒有留言:
張貼留言