我收到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的信息中找到一些关于这一点的信息,但没有成功。


当前回答

 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>

其他回答

检查network_security_config.xml中的域是否正确。

在我的例子中,我使用了一个命令——外部的ionic cordova,它改变了域。

解决方案1

删除--外部

解决方案2

在network_security_config.xml中编辑域

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">
       YOUR_DOMAIN (ex: localhost)
    </domain>
</domain-config>
</network-security-config>

尝试使用“https://”而不是“http://”来访问URL

好吧,我已经想通了。这是由于我添加了Manifest参数android:targetSandboxVersion=“2”,因为我们也有即时应用程序版本-它应该确保用户从即时应用程序升级到常规应用程序后,不会在传输过程中丢失数据。然而,模糊的描述表明:

指定此应用程序要使用的目标沙盒。更高的sanbox版本将具有更高的安全级别。此属性的默认值为1。

显然,它还增加了新级别的安全策略,至少在Android 8上如此。

在开发应用程序时,我也遇到了同样的“Cleartext HTTP流量不允许”错误。我在应用程序中使用改装2进行网络调用,我有两个项目环境(开发和生产)。我的生产域具有带有HTTPS调用的SSL证书,而开发人员没有HTTPS。在构建风格中添加了配置。但当我改为dev时,这个问题就会触发。所以我添加了以下解决方案。

我已在清单中添加了明文流量

 android:usesCleartextTraffic="true"

然后我在改装配置类OKHttp创建时添加了一个连接规范。

 .connectionSpecs(CollectionsKt.listOf(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))

完成OkHttpClient创建如下

OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .readTimeout(10, TimeUnit.SECONDS)
        .connectTimeout(10, TimeUnit.SECONDS)
        .cache(null)
        .connectionSpecs(CollectionsKt.listOf(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))
        .addInterceptor(new NetworkInterceptor(context))
        .addInterceptor(createLoggingInterceptor())
        .addInterceptor(createSessionExpiryInterceptor())
        .addInterceptor(createContextHeaderInterceptor())
        .build();

您可能只想在调试时允许明文,但要保留在生产中拒绝明文的安全优势。这对我很有用,因为我在不支持https的开发服务器上测试我的应用程序。以下是如何在生产中强制https,但在调试模式下允许明文:

在build.gradle中:

// Put this in your buildtypes debug section:
manifestPlaceholders = [usesCleartextTraffic:"true"]

// Put this in your buildtypes release section
manifestPlaceholders = [usesCleartextTraffic:"false"]

在AndroidManifest.xml中的应用程序标记中

android:usesCleartextTraffic="${usesCleartextTraffic}"