从Android 9.0(API级别28)开始,默认情况下限制了明文流量的网络请求,对未加密流量不再信任,直接放弃请求,因此http的url均无法在webview中加载,https 不受影响。
解锁正确姿势
首先保证App申明了网络权限
<uses-permission android:name="android.permission.INTERNET" />
- 1
解决办法(1):WebView可以使用
在Application中打开一个开关
<manifest ...>
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
解决办法(2):【推荐】
res 下新建 xml 目录,创建文件:network_security_config.xml ,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
- 1
- 2
- 3
- 4
在 AndroidManifest.xml 的 application 标签添加配置:
<manifest ...>
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
解决办法(3):
服务器和本地应用都改用 https
解决办法(4):
targetSdkVersion 降级回到 27