【问题标题】:Android 11: Cleartext communication not enabled for client [duplicate]Android 11:未为客户端启用明文通信 [重复]
【发布时间】:2021-04-15 21:48:04
【问题描述】:

我正在创建一个 android 应用程序,用户必须通过 otp 登录并获取 otp 必须向 HTTP url 发出请求。

除了 ANDROID 11 之外,它在所有 android 版本中都能完美运行,说明:

CLEARTEXT communication not enabled for client

我添加了一个网络安全配置并在清单文件中设置了usesCleartextTraffic="true"。网络安全配置适用于 android 9 和 10,但不适用于 android 11。我正在使用 OKHTTP 发出请求。

任何帮助将不胜感激。

【问题讨论】:

  • 不,我试过这个。我通过以下答案解决了这个问题
  • 添加 ConnectionSpec.CLEARTEXT 是我链接到的问题的答案之一。

标签: java android


【解决方案1】:

好的,我解决了它并保持这个问题存在,以便它可以帮助有同样问题的人。我通过更改解决了它 OkHttpClient 客户端 = 新的 OkHttpClient()

OkHttpClient 客户端 = 新 OkHttpClient.Builder() .connectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT,ConnectionSpec.MODERN_TLS)) .build();

【讨论】:

    【解决方案2】:

    将此添加到AndroidManifest.xml的应用程序标签

    android:usesCleartextTraffic="true"
    

    最后的样子,

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.App"
        android:usesCleartextTraffic="true"> // add this line
    

    【讨论】:

    • 这不是我尝试过的解决方案,它在 android 11 中无法发送请求。必须添加其他参数才能发出网络请求
    猜你喜欢
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2013-12-14
    • 1970-01-01
    相关资源
    最近更新 更多