dav*_*d.s 782 https android http
我收到了Android 8用户的报告,我的应用(使用后端Feed)没有显示内容.经过调查,我发现在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及更低版本上一切正常,我没有设置android:usesCleartextTrafficManifest(并设置它true没有帮助,无论如何这是默认值),我也没有使用网络安全信息.如果我打电话NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(),它会返回falseAndroid 8,true对于旧版本,使用相同的apk文件.我试图在关于Android O的Google信息上找到一些提及,但没有成功.
Hri*_*dam 1677
根据网络安全配置 -
从Android 9.0(API级别28)开始,默认情况下禁用明文支持.
另外看看 - https://koz.io/android-m-and-the-war-on-cleartext-traffic/
选项1 -
创建文件res/xml/network_security_config.xml -
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">domain.com (to be adjusted)</domain>
    </domain-config>
</network-security-config>
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:networkSecurityConfig="@xml/network_security_config"
        ...>
        ...
    </application>
</manifest>
选项2 -
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>
另外,正如@ david.s的回答指出的那样android:targetSandboxVersion也是一个问题 -
根据Manifest Docs -
android:targetSandboxVersion此应用程序要使用的目标沙箱.沙箱版本号越高,安全级别越高.其默认值为1; 您也可以将其设置为2.将此属性设置为2会将应用程序切换到另一个SELinux沙箱.以下限制适用于2级沙箱:
usesCleartextTrafficNetwork Security Config中的默认值为false.- 不允许共享Uid.
选项3 -
如果你有android:targetSandboxVersion,<manifest>然后减少它1
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest android:targetSandboxVersion="1">
    <uses-permission android:name="android.permission.INTERNET" />
    ...
</manifest>
byO*_*nti 104
在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
Tyl*_*ler 66
您可能只想在调试时允许明文,但要保留在生产中拒绝明文的安全性.这对我很有用,因为我针对不支持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中的application标签中
android:usesCleartextTraffic="${usesCleartextTraffic}"
Pab*_*rra 66
我在Android 9中的问题是使用http从这个答案的解决方案导航到域上的webview
<application 
    android:networkSecurityConfig="@xml/network_security_config"
    ...>
和:
RES/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>
eli*_*eli 57
将您的网址从HTTP更改为HTTPS ;
它工作了!!!
小智 32
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">***Your URL(ex: 127.0.0.1)***</domain>
    </domain-config>
</network-security-config>
在上面提供的建议中,我提供的URL为 http://xyz.abc.com/mno/
我把它更改为 xyz.abc.com然后它开始工作了.
spa*_*rog 20
它可能对某人有用.
我们最近对Android 9有同样的问题,但我们只需要在WebView中显示一些Urls,没什么特别的.因此添加android:usesCleartextTraffic="true"到Manifest工作,但我们不想为此破坏整个应用程序的安全性.因此,固定在更改链接http到https
sut*_*her 18
好吧,那是?? 不是?? 成千上万的重复, 将其添加到清单中,但基于此的提示,却给您带来额外的好处(也许还有一些背景信息)。
Android具有src-Directory的一种覆盖功能。
默认情况下,
/ app / src / main
但是您可以添加其他目录来覆盖您的AndroidManifest.xml。下面是它的工作原理:
在此文件的内部,您不必将所有规则都放在其中,而只需将您要从/ app / src / main / AndroidManifest.xml中覆盖的规则放进去
这是一个示例,显示请求的CLEARTEXT-Permission的外观:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.yourappname">
    <application
            android:usesCleartextTraffic="true"
            android:name=".MainApplication"
            android:label="@string/app_name"
            android:icon="@mipmap/ic_launcher"
            android:allowBackup="false"
            android:theme="@style/AppTheme">
    </application>
</manifest>
有了这些知识,您现在就可以轻松地将1,2,3作为重载权限的依据,具体取决于您的调试| 主| 释放环境。
这样做的最大好处是……您的生产清单中没有调试资料,并且您可以保持简单易维护的结构
cre*_*der 16
我已经从已经存在的 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"
    >
那么这个错误明文HTTP流量到overlay.openstreetmap.nl不允许在android 9和10中对我来说消失了。我希望这也适用于android 8如果它有帮助你不要忘记投票谢谢
Eri*_*gel 12
对于React Native项目
它已在RN 0.59上修复。您可以找到从0.58.6到0.59的升级差异。 您可以在不升级RN版本的情况下应用它,请按照以下步骤操作:
创建文件:
Android设备/应用/ src目录/ 调试 /res/xml/react_native_config.xml -
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="false">localhost</domain>
    <domain includeSubdomains="false">10.0.2.2</domain>
    <domain includeSubdomains="false">10.0.3.2</domain>
  </domain-config>
</network-security-config>
Android设备/应用/ src目录/ 调试 /AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools">
  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  <application tools:targetApi="28"
      tools:ignore="GoogleAppIndexingWarning" 
      android:networkSecurityConfig="@xml/react_native_config" />
</manifest>
检查接受的答案以了解根本原因。
小智 12
将 ... android:usesCleartextTraffic="true" ... 添加到您的清单文件似乎可以解决问题,但它会对数据完整性构成威胁。
For security reasons I used manifest placeholders with android:usesCleartextTraffic inside the manifest file (like in Option 3 of the accepted answer i.e @Hrishikesh Kadam's response) to only allow cleartext on debug environment.
Inside my build.gradle(:app) file, I added a manifest placeholder like this:
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            manifestPlaceholders.cleartextTrafficPermitted ="true"
        }
    }
Note the placeholder name cleartextTrafficPermitted at this line above
            manifestPlaceholders.cleartextTrafficPermitted ="true"
Then in my Android Manifest, I used the same placeholder ...
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="${cleartextTrafficPermitted}"
        ...>
        ...
    </application>
</manifest>
With that, cleartext traffic is only permitted under the debug environment.
小智 12
最简单的解决方案 [Xamarin Form]
安卓版
Android Project,然后单击Properties,AssemblyInfo.cs并粘贴此代码: 
[assembly: Application(UsesCleartextTraffic =true)]
对于 iOS
使用NSAppTransportSecurity:
您必须将NSAllowsArbitraryLoads密钥设置为文件中的字典YES下。NSAppTransportSecurityinfo.plist
<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>
Jac*_*cob 12
我建议添加开发和生产网络配置:
添加res/xml/network_security_config_dev.xml
<?xml version="1.0" encoding="utf-8"?>
 <network-security-config>
    <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">10.0.2.2</domain>
 </domain-config>
</network-security-config>
添加res/xml/network_security_config_prod.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="false">
    <domain includeSubdomains="true">yourdomain.com</domain>
  </domain-config>
</network-security-config>
在 Gradle 脚本(在 android studio 中)下,找到build.gradle ( android.app ) 并查找buildTypes:发布和调试(如果不存在则创建):
buildTypes {
release {
    minifyEnabled false
    manifestPlaceholders.securityConfig = "@xml/network_security_config_prod"
 }
 debug {
    manifestPlaceholders.securityConfig = "@xml/network_security_config_dev"
 }
}
在 AndroidManifest.xml 中使用securityConfig占位符,如下所示(在build.gradle中定义):
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:networkSecurityConfig="${securityConfig}"   <------- here
好的,我已经弄明白了.这是由于Manifest参数android:targetSandboxVersion="2",我添加了因为我们也有Instant App版本 - 它应该确保用户从Instant App升级到普通应用程序,他不会通过传输丢失他的数据.但是,模糊的描述表明:
指定此应用程序要使用的目标沙箱.更高的sanbox版本将具有更高的安全级别.
此属性的默认值为1.
它显然也增加了新的安全策略,至少在Android 8上.
虽然对我来说有效的答案是@PabloCegarra:
<?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>
您可能会收到有关以下内容的安全警告cleartextTrafficPermitted="true"
如果您知道“白名单”的域,您应该混合接受的答案和上面的答案:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="false">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">books.google.com</domain>
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </domain-config>
</network-security-config>
此代码对我有用,但我的应用程序只需要从 books.google.com 检索数据。这样安全警告就消失了。
 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步-->>创建Drawable>>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
当然需要Internet许可(duh ..):
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
注意:通常,程序集级属性会添加到您的AssemblyInfo.cs文件中,但会添加到作品下方using和上方的任何文件namespace。
然后在您的Application子类上(如果需要,请创建一个),可以添加NetworkSecurityConfig对Resources/xml/ZZZZ.xml文件的引用:
#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>
您还可以在UsesCleartextTraffic上使用参数ApplicationAttribute:
#if DEBUG
[Application(AllowBackup = false, Debuggable = true, UsesCleartextTraffic = true)]
#else
[Application(AllowBackup = true, Debuggable = false, UsesCleartextTraffic = true))]
#endif
在开发我的应用程序时,我也遇到了相同的“不允许明文 HTTP 流量”错误。我在应用程序中使用 Retrofit2 进行网络调用,并且有两个项目环境(开发和生产)。我的生产域具有带 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();
2019 年 12 月更新 ionic - 4.7.1
<manifest xmlns:tools=“http://schemas.android.com/tools”>
<application android:usesCleartextTraffic=“true” tools:targetApi=“28”>
请在android manifest .xml文件中添加以上内容
离子的以前版本
确保您的config.xmlIonic 项目中有以下内容:
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
            <application android:networkSecurityConfig="@xml/network_security_config" />
            <application android:usesCleartextTraffic="true" />
        </edit-config>
运行 ionic Cordova build android。它在平台下创建 Android 文件夹
打开 Android Studio 并打开我们项目 project-platforms-android 中的 Android 文件夹。让它停留几分钟,以便它构建 gradle
后gradle build完成后,我们得到了一些错误的,包括minSdVersion在manifest.xml。现在我们所做的只是<uses-sdk android:minSdkVersion="19" />从manifest.xml.
确保将其从两个位置移除:
AndroidManifest.xml。AndroidManifest.xml。现在尝试再次构建gradle,现在它构建成功
确保在 App → manifest → 的 Application 标签中有以下内容Androidmanifest.xml:
<application
android:networkSecurityConfig="@xml/network_security_config"  android:usesCleartextTraffic="true" >
打开network_security_config(app → res → xml →network_security_config.xml)。
添加以下代码:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">xxx.yyyy.com</domain>
    </domain-config>
</network-security-config>
这里 xxx.yyyy.com是您的 HTTP API 的链接。确保在 URL 之前不包含任何 Http。
注意:现在使用 Android Studio (Build -- Build Bundle's/APK -- Build APK) 构建应用程序,现在您可以使用该应用程序并且它在 Android Pie 中运行良好。如果您尝试使用 ionic Cordova build android 构建应用程序,它会覆盖所有这些设置,因此请确保您使用 Android Studio 构建项目。
如果您安装了任何旧版本的应用程序,请卸载它们并尝试一下,否则您将遇到一些错误:
应用未安装
| 归档时间: | 
 | 
| 查看次数: | 413356 次 | 
| 最近记录: |