Android高版本联网失败报错:Cleartext HTTP traffic to xxx not permitted解决方法

前言:为保证用户数据和设备的安全,Google针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的连接,因此运行 Android P 系统的安卓设备无论是接收或者发送流量,未来都不能明码传输,需要使用下一代(Transport Layer Security)传输层安全协议,而 Android Nougat 和 Oreo 则不受影响。

因此在Android P 使用HttpUrlConnection进行http请求会出现以下异常

W/System.err: java.io.IOException: Cleartext HTTP traffic to **** not permitted
使用OKHttp请求则出现

java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by network security policy
在Android P系统的设备上,如果应用使用的是非加密的明文流量的http网络请求,则会导致该应用无法进行网络请求,https则不会受影响,同样地,如果应用嵌套了webview,webview也只能使用https请求。

针对这个问题,有以下三种解决方法:

(1)APP改用https请求

(2)targetSdkVersion 降到27以下

(3)更改网络安全配置

前面两个方法容易理解和实现,具体说说第三种方法,更改网络安全配置。

1.在res文件夹下创建一个xml文件夹,然后创建一个network_security_config.xml文件,文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
2.接着,在AndroidManifest.xml文件下的application标签增加以下属性:

<application
...
android:networkSecurityConfig="@xml/network_security_config"
...
/>
完成,这个时候App就可以访问网络了。

原文地址:https://www.cnblogs.com/yelanggu/p/10739485.html

时间: 2024-08-03 09:25:26

Android高版本联网失败报错:Cleartext HTTP traffic to xxx not permitted解决方法的相关文章

Cleartext HTTP traffic to xxx not permitted解决办法

,为保证用户数据和设备的安全,针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的连接,因此运行 Android P 系统的安卓设备无论是接收或者发送流量,未来都不能明码传输,需要使用下一代(Transport Layer Security)传输层安全协议,而 Android Nougat 和 Oreo 则不受影响.因此在Android P 使用HttpUrlConnection进行http请求

很爽的一点。用Mysql5.x以上版本出现报错#1929 Incorrect datetime value: &#39;&#39; for column &#39;createtime&#39; 解决方法

我的mysql安装后,保存删除表数据总是出现#1929 Incorrect datetime value: '' for column 'createtime' 的报错提醒,导致不能删除表里数据: 原因: 5以上的版本如果时期时间是空值应该要写NULL: 官方解释说:得知新版本mysql对空值插入有"bug", 要在安装mysql的时候去除默认勾选的enable strict SQL mode 那么如果我们安装好了mysql怎么办了,解决办法是更改mysql中的配置 my.ini 解决

报错:Could not launch &#39;app name&#39;,解决方法

如果你也出現了“Could not launch 'app name'”,No such file or directory (/Users/apple/Library/Developer/Xcode/DerivedData/mytest-ejkagqxooxgmtdfsdoygtyzflibe/Build/Products/Debug-iphoneos/mytest.app/mytest) 这样的讯息的話,有几个处理方法1.刪除DerivedData資料夾法1.1 完全离开Xcode,然后再做

在WSL中安装swool报错 error: unrecognized command line option &#39;-V&#39; 的解决方法

Windows Subsystem for Linux Ubuntu中使用pecl安装swool时遇到错误"error: C compiler cannot create executables"."error: unrecognized command line option '-V'" 解决方法 先看解决方法, 将gcc版本降低到4.8即可,因为高版本gcc取消了-V参数: sudo apt install gcc-4.8 -y sudo update-alte

python中引入包的时候报错AttributeError: module &#39;sys&#39; has no attribute &#39;setdefaultencoding&#39;解决方法?

python中引入包的时候报错:import unittestimport smtplibimport timeimport osimport sysimp.reload(sys)sys.setdefaultencoding('utf-8') AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法: 1.python2中解决方法:reload(sys)sys.setdefaultencoding('utf-8'

python 编码报错问题 &#39;ascii&#39; codec can&#39;t encode characters 解决方法

python在安装时,默认的编码是ascii, 当程序中出现非ascii编码时,python的处理常常会报这样的错 'ascii' codec can't encode characters python没办法处理非ascii编码的, 此时需要自己设置将python的默认编码,一般设置为utf8的编码格式. 查看python的默认编码 print sys.getdefaultencoding() 解决方法一(已通过验证,顺带也解决了我之前字符前一直加u的问题) 在python安装目录下,进入\P

Centos6 下启动httpd报错 Could not reliably determine the server&#39;s解决方法

在启动httpd的时候报错: 修改/etc/httpd/conf/httpd.conf 配置,去掉ServerName 前的#(或者手动添加ServerName localhost:80)然后重启httpd服务: Centos6 下启动httpd报错 Could not reliably determine the server's解决方法

keil 通过JTAG下载程序 报错:error: flash download failed - &quot;cortex-m3&quot;的解决方法

硬件环境: nxp lpc1788fbd208核心板 软件环境: keil uvision4 segger j-flash arm v4.50 其他: 芯片选择正确 jtag模式 现象:之前用JFlashARM.exe下载了程序,在没有擦除程序的情况下,JTAG模式下用keil下载程序 报错 error: flash download failed - "cortex-m3" 原因:用jFlashARM.exe下载程序后,没有擦除程序 解决:擦除lpc1788 上的程序,然后在keil

Myeclipse报错-Java compiler level does not match 完美解决方法

从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclipse报Description  Resource Path Location Type Java compiler level does not match the version of the installed Java project facet.出现这个问题的原因是因为,eclipse/myeclipse的jdk编译版本与出现问题的项目JDK编译版本不一致所导致! MyEclipse 1.先设置好jdk,需要确定 项目