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

}
8
  • INTERNET permission set in Manifest? Commented Jun 7, 2018 at 10:52
  • @Christopher Already given the permission Commented Jun 7, 2018 at 10:53
  • 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. Commented Jun 7, 2018 at 11:01
  • yes getting response on postman but i issue with this. Commented Jun 7, 2018 at 11:04
  • 1
    Do you by any chance dispose the compositeDisposable? Commented Jun 7, 2018 at 13:51

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.