我已经定义了一个自定义网络安全配置,并将其作为推荐的here包含在我的清单中
RES / XML / network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">127.0.0.1</domain>
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
这是我的Android.manifest:
<application android:icon="@drawable/icon"
android:allowBackup="false"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:persistent="true" >
尝试通过HTTP与127.0.0.1进行通信时,即使有这些更改,我也会在logcat中看到这一点:
08-09 10:50:34.395 30791 3607 D NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
08-09 10:50:34.397 30791 3607 D NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
08-09 10:50:34.401 30791 3607 W DownloadManager: [647] Stop requested with status HTTP_DATA_ERROR: Cleartext HTTP traffic to 127.0.0.1 not permitted
08-09 10:50:34.402 30791 3607 D DownloadManager: [647] Finished with status WAITING_TO_RETRY
编辑:更新(2018年8月21日)在“修复”此问题后,似乎在安装应用程序几个小时后,DownloadManager神秘地停止接受明文HTTP。
我无法解释发生了什么。如果我重新启动设备,一段时间内工作正常,那么DownloadManager会再次拒绝明文。我把它写成一个Android P bug,我希望得到解决。
编辑2:更新(2019年1月17日)我的pixel2现在运行android 9与2019年1月5日的补丁,我不再看到问题(到目前为止)。我猜这个问题已经从8月开始在一些补丁中解决了。
创建XML res / xml / network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
在AndroidManifest.xml中的标记Application中引用此文件。喜欢:
android:networkSecurityConfig="@xml/network_security_config"