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


当前回答

在我的情况下,URL也不能在浏览器中工作。

我向https://www.google.com/

webView.loadUrl("https://www.google.com/")

这对我有用。

其他回答

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

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

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

在AndroidManifest中,我找到了这个参数:

android:networkSecurityConfig="@xml/network_security_config"

而@xml/network_security_config在network_security-config.xml中定义为:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <!--Set application-wide security config using base-config tag.-->
    <base-config cleartextTrafficPermitted="false"/>
</network-security-config>  

只是我将cleartextTrafficPermitted更改为true

 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>

如果您正在使用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>

这对我有用!

创建文件-res/xml/network_security.xml

在network_security.xml->中

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

打开AndroidManifests.xml:

 android:usesCleartextTraffic="true" //Add this line in your manifests

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">