Retrofit / okHttpClient Limiting the timeout to 10 seconds anyway

287 Views Asked by At

I did the custom OkHttpClient setup, but my app still can't wait more than 10 seconds in a request, then gets a timeout with 502 error code.

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .callTimeout(30, TimeUnit.SECONDS)
            .build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

What could be happening that is still limiting my timeout?

I tried changing my server backend settings, but it looks like the problem is directly in my app, looks like the timeout doesn't go further than 10 seconds, because the same request made in Postman can be done without problems.

1

There are 1 best solutions below

1
On

A similar problem occurred to us, and it turned out to be related to DNS resolution when using a qualified domain name.

If you're making requests to a server with a qualified domain name, it's possible that the DNS resolution is causing the timeout, answering back a 502 error code. In our case, the issue was resolved when we discovered that the DNS acting as a proxy had a timeout set to 30 seconds.

You may want to check if you're using a qualified domain name in your API requests and if so, consider increasing the timeout in your DNS server. We discovered the problem when we tried reaching the endpoint by its IP address instead of the domain and it worked.

This resolved the problem for us and requests that were timing out before started working as expected.