25

I have already defined a custom network security config and included it in my manifest as recommended 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>

and this is in my 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" >

Even with these changes when attempting to communicate via HTTP to 127.0.0.1 I see this in 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

EDIT: Update (21 Aug 2018) after "fixing" this issue it seems that a couple of hours after the app is installed the DownloadManager mysteriously stops accepting cleartext HTTP.

I can't explain what's happening. If I reboot the device things work great for a period of time then DownloadManager refuses cleartext again. I'm writing this off as an Android P bug that I hope gets resolved.

EDIT 2: Update (17 Jan 2019) my pixel2 is now running android 9 with a patch from 5 Jan 2019 and I no longer see the issue (so far). I'm guessing this got resolved in some patch since August.

4

3 回答 3

46

创建一个 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 内的标记应用程序中引用此文件。喜欢:

android:networkSecurityConfig="@xml/network_security_config"
于 2018-08-10T11:41:22.843 回答
0

打开清单 在此处输入图像描述

添加 android:usesCleartextTraffic="true" 在此处输入图像描述

于 2021-06-03T02:48:07.750 回答
-1

将 BASE_URL 协议从http://...替换为https://

于 2020-06-09T08:57:49.870 回答