HTTP 失败:java.net.SocketException:套接字关闭改造 [英] HTTP FAILED: java.net.SocketException: Socket closed Retrofit

查看:217
本文介绍了HTTP 失败:java.net.SocketException:套接字关闭改造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试了多种方法仍然面临这个问题

tried with multiple ways still facing this issue as

我使用 RxJava 和 Retrofit 来完成我所有的网络操作.下面是我的代码.

I am using RxJava and Retrofit to do all my network operations. Below is my code.

<-- HTTP 失败:java.net.SocketException:套接字已关闭

<-- HTTP FAILED: java.net.SocketException: Socket closed

Service.kt

@GET("/v1/contact/{id}")
fun getContactDetails(@Path("id") id:String): Flowable<ContactDetailResponse>

AccountRepositoryImpl.kt

 override fun contactDetails(contactId:String) {
    mContactResponse.loading(true)
    remote.getContactDetails(contactId)
            .performOnBackOutOnMain(scheduler)
            .subscribe({
                data -> mContactResponse.success(data)
            },{
                error -> mContactResponse.failed(error)
            })
            .addTo(compositeDisposable)
}

Extensions.kt

fun <T> Flowable<T>.performOnBackOutOnMain(scheduler: Scheduler): 
Flowable<T> {
  return this.subscribeOn(scheduler.io())
        .observeOn(scheduler.mainThread())
}

NetworkModule.kt

@Module
class NetworkModule(val key: String, val secret: String) {

 @Provides
 @Singleton
 fun providesRetrofit(jacksonConverterFactory: JacksonConverterFactory,
                     rxJava2CallAdapterFactory: 
 RxJava2CallAdapterFactory,
                     okHttpClient: OkHttpClient,
                     baseUrl: String): Retrofit {

    return Retrofit.Builder().baseUrl(baseUrl)
            .addConverterFactory(jacksonConverterFactory)
            .addCallAdapterFactory(rxJava2CallAdapterFactory)
            .client(okHttpClient)
            .build()
 }

  @Provides
  @Singleton
  fun providesOkHttpClient(cache: Cache, httpInterceptor: 
  HttpLoggingInterceptor): OkHttpClient {

    val client = OkHttpClient.Builder()
            .cache(cache)
            .addInterceptor(httpInterceptor)
            .retryOnConnectionFailure(true)
            .connectTimeout(100, TimeUnit.SECONDS)
            .writeTimeout(100, TimeUnit.SECONDS)
            .readTimeout(100, TimeUnit.SECONDS)


    return client.build()
  }

}

推荐答案

你可以像下面这样创建你的OkHttp Client,我在低网络中遇到了socket关闭异常,在OkhttpClient issue resloved中添加以下类似后

You can Create your OkHttp Client like below, I have faced socket closed exception in the low network, after adding following like in OkhttpClient issue resloved

connectionPool(ConnectionPool(0, 1, TimeUnit.NANOOSECONDS))
协议(listOf(Protocol.HTTP_1_1))

        OkHttpClient.Builder().apply {
        .connectionPool(ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
        .protocols(listOf(Protocol.HTTP_1_1))
        connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
        writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
        readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
        retryOnConnectionFailure(false)
        }.build()

这篇关于HTTP 失败:java.net.SocketException:套接字关闭改造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆