HTTP FAILED: java.net.SocketException: Socket closed Retrofit
Asked Answered
O

1

2

tried with multiple ways still facing this issue as

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

<-- 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)
}

Extentions.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()
  }

}
Orpheus answered 7/6, 2018 at 10:47 Comment(8)
INTERNET permission set in Manifest?Cloven
@Cloven Already given the permissionOrpheus
Ok, I think we need a more detailed error description. SocketException has several causes. Does the same HTTP request work with a different tool, e.g. Postman? To ensure that the cause is not your backend.Cloven
yes getting response on postman but i issue with this.Orpheus
i have also checked this link but not getting where to do it : #40823634Orpheus
yes getting response on postman On your PC? Use a browser on your device.Xerox
Do you by any chance dispose the compositeDisposable?Duncandunce
can you show me your baseUrl ?Decrepit
E
0

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.NANOSECONDS))
protocols(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()
Earthiness answered 2/9, 2020 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.