cleartext HTTP traffic not permitted

简介: cleartext HTTP traffic not permitted

异常描述

在使用Android P(9.0)的机型上,如果我们的app使用的是Okhttp或HttpUrlConnection做网络请求时,会在logcat中报以下的错误.


原因

从android 9.0开始,网络请求默认不支持没有加密的明文传输.

解决方法

方法1

将http改为https.

方法2

在res/xml目录创建一个资源文件.

如果没有xml目录,先创建一个.

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true"/>
</network-security-config>

在AndroidManifest.xml清单文件的Application标签中添加networkSecurityConfig属性,引用上面创建的

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:networkSecurityConfig="@xml/network_security_config"
                    ... >
        ...
    </application>
</manifest>

这样我们的app就能正常的使用http请求了。


参考:https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted

https://developer.android.google.cn/training/articles/security-config#CleartextTrafficPermitted

相关文章
|
2月前
Fiddler——This is a CONNECT tunnel, through which encrypted HTTPS traffic flows
Fiddler——This is a CONNECT tunnel, through which encrypted HTTPS traffic flows
37 0
Fiddler——This is a CONNECT tunnel, through which encrypted HTTPS traffic flows
remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2
remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2
4163 0
|
4月前
|
前端开发 对象存储 数据安全/隐私保护
阿里云前端直传has been blocked by CORS policy: Response to preflight request doesn‘t pass access control
阿里云前端直传has been blocked by CORS policy: Response to preflight request doesn‘t pass access control
435 2
|
关系型数据库 PostgreSQL Ruby
HTTP 502: Whoops, GitLab is taking too much time to respond.
最近有台云上的服务器需要释放,然后上面跑的 gitlab 也要挪个地方,如在 docker 内运行,gitlab 镜像大约 1.56G,需占用 4G 以上的内存,因资源有限,于是借在其他的服务器上搭建环境(可用内存小于4G),然鹅启动的时候莫名出现 502,Excuse me?接着搜了一些 issue 博客上的解决方案(如修改端口、重启或 hup 某个服务)无果,后来在调整的过程中从日志里发现了一些信息。
11993 0
|
开发工具 数据安全/隐私保护
The request you have made requires authentication. (HTTP 401)
The request you have made requires authentication. (HTTP 401)
1294 0
The request you have made requires authentication. (HTTP 401)
|
开发工具 git
fatal: protocol ‘“https‘ is not supported
fatal: protocol ‘“https‘ is not supported
|
iOS开发 MacOS
curl: (4) A requested feature, protocol or option was not found
curl: (4) A requested feature, protocol or option was not found
263 0
curl: (4) A requested feature, protocol or option was not found
|
开发工具 git
fatal: protocol ‘–https‘ is not supported
fatal: protocol ‘–https‘ is not supported
454 0
fatal: protocol ‘–https‘ is not supported
requests InsecureRequestWarning: Unverified HTTPS request is being made.
requests InsecureRequestWarning: Unverified HTTPS request is being made.
1072 0
curl: (1) Protocol "https" not supported or disabled in libcurl
curl: (1) Protocol "https" not supported or disabled in libcurl
291 0