gpt4 book ai didi

android - SocketException : java.net.SocketException:套接字已关闭发布版本

转载 作者:行者123 更新时间:2023-12-05 05:48:02 25 4
gpt4 key购买 nike

我们在 Android 网络请求方面遇到问题,更确切地说是随机接收

SocketException:
java.net.SocketException: socket is closed
at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:551)

请求似乎没有超时,异常很快抛出,而且似乎只有在release模式下,我们猜测可能与ProGuard有关。

我们使用 Retrofit、Moshi 和 RxAndroid 来处理我们的网络请求。有没有人遇到过这样的问题?

我们的依赖和它们的版本:

implementation ‘io.reactivex.rxjava2:rxandroid:2.1.1’
implementation ‘io.reactivex.rxjava2:rxjava:2.2.9’
implementation ‘com.squareup.retrofit2:adapter-rxjava2:2.5.0’
//Moshi
def moshiVersion = “1.12.0”
implementation “com.squareup.moshi:moshi:$moshiVersion”
kapt “com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion”
implementation “com.squareup.retrofit2:converter-moshi:2.9.0"
implementation “com.squareup.moshi:moshi-kotlin:1.8.0”
//Networking
implementation ‘com.squareup.okhttp3:logging-interceptor:3.12.1’
implementation ‘com.squareup.retrofit2:retrofit:2.6.4’
implementation ‘com.squareup.retrofit2:converter-scalars:2.1.0’

我们所有的模型都带有@Keep 注释:

@Keep
class ModelDTO(
@field:Json(name = “field1")
var field1: String? = null,
@field:Json(name = “field2”)
var field2: List<String>? = null
)
HttpClient + Retrofit:
val client = OkHttpClient.Builder()
.connectTimeout(300, TimeUnit.SECONDS)
.writeTimeout(300, TimeUnit.SECONDS)
.readTimeout(300, TimeUnit.SECONDS)
client.retryOnConnectionFailure(true)
.addNetworkInterceptor { chain ->
val request = chain.request().newBuilder().addHeader(“Connection”, “close”).build()
chain.proceed(request)
}
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
val retrofitBuilder = Retrofit.Builder()
retrofitBuilder.baseUrl(BuildConfig.BASE_URL)
retrofitBuilder.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
.addCallAdapterFactory(
RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io())
)

我们的 Gradle 设置:

        release {
debuggable false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
consumerProguardFiles 'proguard-rules.pro'
}

最佳答案

经过多次尝试,我们解决了这个问题:

.connectionPool(ConnectionPool(0, 1, TimeUnit.NANOSECONDS)).protocols(listOf(协议(protocol).HTTP_1_1))

这是添加到 OkHttpClient :) 工作起来非常棒!

关于android - SocketException : java.net.SocketException:套接字已关闭发布版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70877944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com