我收到Android 8用户的报告,我的应用程序(使用后端提要)不显示内容。经过调查,我发现Android 8上发生了以下异常:

08-29 12:03:11.246 11285-11285/ E/: [12:03:11.245, main]: Exception: IOException java.io.IOException: Cleartext HTTP traffic to * not permitted
at 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.connect(HttpURLConnectionImpl.java:127)
at com.deiw.android.generic.tasks.AbstractHttpAsyncTask.doConnection(AbstractHttpAsyncTask.java:207)
at com.deiw.android.generic.tasks.AbstractHttpAsyncTask.extendedDoInBackground(AbstractHttpAsyncTask.java:102)
at com.deiw.android.generic.tasks.AbstractAsyncTask.doInBackground(AbstractAsyncTask.java:88)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)

(我删除了包名、URL和其他可能的标识符)

在Android 7和更低版本上,一切都正常,我没有在Manifest中设置Android:usesCleartextTraffic(并且将其设置为true没有帮助,这是默认值),也没有使用网络安全信息。如果我调用NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(),它将使用相同的apk文件,为Android 8返回false,为旧版本返回true。我试图在谷歌关于Android O的信息中找到一些关于这一点的信息,但没有成功。


当前回答

我已经从已经存在的android清单文件中删除了这行

 android:networkSecurityConfig="@xml/network_security_config" 

并添加了

android:usesCleartextTraffic="true"

清单中的应用程序标记

<application
    android:usesCleartextTraffic="true"
    android:allowBackup="true"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    >

然后这个错误Cleartext HTTP流量到overlay.openstreetmap.nl不允许在我的android 9和10中消失。我希望这也适用于android 8,如果它有帮助,请不要忘记投票谢谢

其他回答

明文是指未加密或未打算加密的任何传输或存储的信息。

当应用程序使用明文网络流量(如HTTP(而非https))与服务器进行通信时,可能会增加黑客入侵和篡改内容的风险。第三方可以注入未经授权的数据或泄露有关用户的信息。这就是为什么鼓励开发人员只保护流量,例如HTTPS。下面是如何解决这个问题的实现和参考。

简单易行的解决方案[Xamarin Form]

适用于Android

转到Android项目,然后单击财产,

打开AssemblyInfo.cs并在那里粘贴以下代码:[程序集:应用程序(UsesCleartextTraffic=true)]

对于iOS

使用NSAppTransportSecurity:

您必须在info.plist文件中的NSAppsTransportSecurity字典下将NSAllowsArbitraryLoads键设置为YES。

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

 cleartext support is disabled by default.Android in 9 and above

 Try This one I hope It will work fine

1 Step:->  add inside android build gradle (Module:App)
            useLibrary 'org.apache.http.legacy'

  android {
               compileSdkVersion 28
              useLibrary 'org.apache.http.legacy'

          }

然后2步:->清单在清单应用程序标记内添加

<application
    android:networkSecurityConfig="@xml/network_security_config">//add drawable goto Step 4

   // Step --->3  add to top this line  
     <uses-library
        android:name="org.apache.http.legacy"
        android:required="false" />

</application>

//步骤4-->创建可绘制>>Xml文件>>名称为>>network_security_config.Xml

   <?xml version="1.0" encoding="utf-8"?>
   <network-security-config>
      <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
           <certificates src="system" />
        </trust-anchors>
      </base-config>
    </network-security-config>

要将这些不同的答案应用于Xamarin.Android,您可以使用类和汇编级属性,而不是手动编辑AndroidManifest.xml

当然需要互联网许可(duh…):

[assembly: UsesPermission(Android.Manifest.Permission.Internet)]

注意:通常,程序集级属性会添加到AssemblyInfo.cs文件中,但using下面和命名空间上面的任何文件都有效。

然后,在应用程序子类上(如果需要,创建一个),可以添加引用Resources/xml/ZZZZZ.xml文件的NetworkSecurityConfig:

#if DEBUG
[Application(AllowBackup = false, Debuggable = true, NetworkSecurityConfig = "@xml/network_security_config")]
#else
[Application(AllowBackup = true, Debuggable = false, NetworkSecurityConfig = "@xml/network_security_config"))]
#endif
public class App : Application
{
    public App(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer) { }
    public App() { }

    public override void OnCreate()
    {
        base.OnCreate();
    }
}

在Resources/xml文件夹中创建一个文件(如果需要,创建xml文件夹)。

示例xml/network_security_config文件,根据需要进行调整(请参阅其他答案)

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
          <domain includeSubdomains="true">www.example.com</domain>
          <domain includeSubdomains="true">notsecure.com</domain>
          <domain includeSubdomains="false">xxx.xxx.xxx</domain>
    </domain-config>
</network-security-config>

还可以在ApplicationAttribute上使用UsesCleartextTraffic参数:

#if DEBUG
[Application(AllowBackup = false, Debuggable = true, UsesCleartextTraffic = true)]
#else
[Application(AllowBackup = true, Debuggable = false, UsesCleartextTraffic = true))]
#endif

如果您正在使用ionic并在本机http插件期间遇到此错误,则需要执行以下修复-

转到resources/android/xml/network_security_config.xml将其更改为-

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
    </domain-config>
</network-security-config>

这对我有用!