SocketException:java.net.SocketException:套接字已关闭发布版本
SocketException: java.net.SocketException: socket is closed release build
我们在 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'
}
您可以将以下代码添加到 pro-guard 文件:
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
-dontwarn okio.**
但是你用的是R8,包罗万象。
经过多次尝试,我们解决了这个问题:
.connectionPool(ConnectionPool(0, 1, TimeUnit.NANOSECONDS)).protocols(listOf(
协议。HTTP_1_1))
这是添加到 OkHttpClient :) 非常棒!
我们在 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'
}
您可以将以下代码添加到 pro-guard 文件:
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
-dontwarn okio.**
但是你用的是R8,包罗万象。
经过多次尝试,我们解决了这个问题:
.connectionPool(ConnectionPool(0, 1, TimeUnit.NANOSECONDS)).protocols(listOf( 协议。HTTP_1_1))
这是添加到 OkHttpClient :) 非常棒!