Cleartext HTTP traffic to xxx not permitted解决

冷不防 2023-06-22 03:21 66阅读 0赞

从Android 9.0(API级别28)开始,默认情况下限制了明文流量的网络请求,对未加密流量不再信任,直接放弃请求,因此http的url均无法在webview中加载,https 不受影响。

解锁正确姿势

首先保证App申明了网络权限

  1. <uses-permission android:name="android.permission.INTERNET" />
  2. 1
解决办法(1):WebView可以使用

在Application中打开一个开关

  1. <manifest ...>
  2. <application
  3. ...
  4. android:usesCleartextTraffic="true"
  5. ...>
  6. ...
  7. </application>
  8. </manifest>
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5
  14. 6
  15. 7
  16. 8
解决办法(2):【推荐】

res 下新建 xml 目录,创建文件:network_security_config.xml ,内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <network-security-config>
  3. <base-config cleartextTrafficPermitted="true" />
  4. </network-security-config>
  5. 1
  6. 2
  7. 3
  8. 4

在 AndroidManifest.xml 的 application 标签添加配置:

  1. <manifest ...>
  2. <application
  3. ...
  4. android:networkSecurityConfig="@xml/network_security_config"
  5. ...>
  6. ...
  7. </application>
  8. </manifest>
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5
  14. 6
  15. 7
  16. 8
解决办法(3):

服务器和本地应用都改用 https

解决办法(4):

targetSdkVersion 降级回到 27

发表评论

表情:
评论列表 (有 0 条评论,66人围观)

还没有评论,来说两句吧...

相关阅读