解决办法总结"/>
Android P Cleartext HTTP traffic to xxx not permitted 解决办法总结
原文链接: Android P Cleartext HTTP traffic to xxx not permitted 解决办法总结.
最近在Android P系统上使用腾讯IM的时候,在聊天界面,发送语音对方能听到,我听不到。发送图片对方能看到,我这里就是一个小框,图片显示不了。打断点调试,错误日志如下:
java.io.IOException: Cleartext HTTP traffic to xxx.x.xxx.xx not permittedat com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)at com.tencent.imsdk.br.run(Unknown Source:78)at java.lang.Thread.run(Thread.java:764)
Google了一下,问题原因是在Android P系统中:
默认情况下启用网络传输层安全协议 (TLS)
也就是说,如果我们的应用是在Android 9或更高版本为目标平台,则默认情况下,是不支持HTTP明文请求的。
在Android P使用Glide加载网络图片也遇到了同样的问题:
java.io.IOException: Cleartext HTTP traffic to img5.duitang not permitted
所以根据原因可以有以下几种修改方法:
1. 将http请求改为https请求
2. 将targetSdkVersion降到28以下
3. 修改AndroidManifest.xml文件
添加"android:usesCleartextTraffic="true"节点,启用域名明文,支持Http请求。
<?xml version="1.0" encoding="utf-8"?>
<manifest ...><uses-permission android:name="android.permission.INTERNET" /><application...android:usesCleartextTraffic="true"...>...</application>
</manifest>
4. 新建res/xml/network_security_config.xml文件,设置网络安全性配置
network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config><!--Set application-wide security config using base-config tag.--><base-config cleartextTrafficPermitted="true"/>
</network-security-config>
在AndroidManifest.xml中,application节点下新增:
android:networkSecurityConfig="@xml/network_security_config"
5.新建res/xml/network_security_config.xml文件,设置网络安全性配置
这个方法跟上面的类似,不同之处在于,方法4将所有域名的http请求都允许了,下面这个方法只允许了指定域名的http请求。
<?xml version="1.0" encoding="utf-8"?>
<network-security-config><domain-config cleartextTrafficPermitted="true"><domain includeSubdomains="true">(Your URL)xxx.x.xxx.xx</domain><domain includeSubdomains="true">x.xxx.xx</domain></domain-config>
</network-security-config>
更多推荐
Android P Cleartext HTTP traffic to xxx not permitted 解决办法总结
发布评论