我在开发应用程序时也遇到了同样的“不允许明文http流量”错误。我在应用程序中使用reformation2进行网络调用,我有两个项目环境(dev&production)。我的生产域有带有https调用的ssl证书,而dev没有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();