android9(api28),从Android 9(API级别28)开始,默认情况下禁用明文支持。

该博客介绍了如何在Android应用中根据不同的构建类型(调试与发布)来选择使用HTTPS或明文流量。在调试模式下,通过在build.gradle中设置manifestPlaceholders使应用允许明文流量,而在生产环境中则禁止。此外,还提到了修改AndroidManifest.xml文件和使用OkHttp时的配置以支持或限制明文流量。
摘要由CSDN通过智能技术生成

在生产环境中强制使用https,但在调试模式下允许使用明文的方法 它仅在api 23+上使用

在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}"

android:usesCleartextTraffic="true"

其它方法“

删除了这一行

android:networkSecurityConfig="@xml/network_security_config"

application添加

android:usesCleartextTraffic="true"

改造配置类OKHttp创建时添加了一个连接规范

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

OkHttpClient okHttpClient = newOkHttpClient.Builder()

.readTimeout(10, TimeUnit.SECONDS)

.connectTimeout(10, TimeUnit.SECONDS)

.cache(null)

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

.addInterceptor(newNetworkInterceptor(context))

.addInterceptor(createLoggingInterceptor())

.addInterceptor(createSessionExpiryInterceptor())

.addInterceptor(createContextHeaderInterceptor())

.build();

api.endv.cn

im.endv.cn

pay.endv.cn

endv.cn

原文:https://www.cnblogs.com/endv/p/13262399.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值