Mustafa Maden
Mustafa Maden

Reputation: 443

Why does it take a long time to get a response from the retrofit in some cases?

Retrofit @get request sometimes It takes a long time to get the answer (10-15 sec.). I am only getting string data. on average 1-2 kb.

No problem when I send request from postman

What do you think is the problem?

RetrofitService.class;

 private val okHttpClient = OkHttpClient().newBuilder()
        .connectTimeout(20, TimeUnit.SECONDS)
        .readTimeout(20, TimeUnit.SECONDS)
        .writeTimeout(20, TimeUnit.SECONDS)
        .build()

private val call = Retrofit.Builder()
    .baseUrl(Constants.LINK)
    .client(okHttpClient)
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
    .build().create(ApiEndpointRXJava::class.java)


fun getRetrofit(query: String, auth: String, queryList: List<String>): Single<List<ListModel>> {
    return  call.getRetrofit(query, auth, queryList)
}

Interface;

    @GET(".../.../...")
    fun getRetrofit(
        @Query("query") query: String,
        @Header("Authorization") authorization: String,
        @Query("queryList") queryList: List<String>
    ): Single<List<ListModel>>

ViewModel.class;

fun getRetrofit(query: String, auth: String, queryList: List<String>) {
    errorData.value = false

    disposable.add(
        retrofit
            .getRetrofit(query, auth, queryList)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(object : DisposableSingleObserver<List<ListModel>>() {
                override fun onSuccess(t: List<ListModel>) {
                    v.value = t
                }

                override fun onError(e: Throwable) {
                    e.value = true
                }
            })
    )
}

Upvotes: 5

Views: 1686

Answers (1)

Anatolii Chub
Anatolii Chub

Reputation: 1300

If sometimes it works fast sometimes with delay - it looks like issue is not in your codebase. Maybe the issue on the server side or in your network connection. To check this case you can use Fiddler or Charles to sniff your application traffic. And if response in the sniffing program will have the same time then issue is not related to your codebase.

Upvotes: 2

Related Questions