Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit custom timeout is not working

I am using retrofit version 2.3.0 and OkHttp version 3.8.1. I am facing a problem that I cannot change timeout for my requests. I was searching for answer in google and there is only one solution - Change timeouts in OkHttpClient and set it to retrofit builder, but it is not working :/. here is a code that I use for changing timeouts:

Retrofit.Builder builder = new Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(GsonConverterFactory.create(gson));

builder.client(new OkHttpClient.Builder()
       .connectTimeout(50000, TimeUnit.MILLISECONDS)
       .readTimeout(50000, TimeUnit.MILLISECONDS).build());

retrofit = builder.build();

But after this requests still lasts for 10 seconds and I get SocketTimoutException

EDIT

I have done some more research and I was testing it with different versions of Retrofit. I stoped on retrofit version 2.0.1 and this version also not allows me to change timeout :/. Have someone got any idea what to do now?

EDIT 2

I have this problem now in two places. I will describe the use case so maybe it will help with finding the solution.

use 1:

I want to download file from remote server. I call web service and I wait for response with file stream. I need to wait longer because web service needs to get file from database and put it into hard drive (there is no possibility to change it). When file is big copying it to hard drive takes time and application receives SocketTimeoutException.

use 2:

I upload file to the server (and it works fine). After upload I need to let web service know that it should load file into the database. I send the request and situation is similar to the use 1, because when file is big, loading takes much time and I get SocketTimeoutException.

UPDATE

I would like to clear up situation. The problem was occurring because I had the second OkHttpClient assigned to the Retrofit which was overriding timeout. If someone will have a similar problem I recommend to check if there is a similar situation to the one I was facing (second OkHttpClient that overrides timeout).

like image 909
Patryk Jabłoński Avatar asked Aug 24 '17 09:08

Patryk Jabłoński


People also ask

How do I change the timeout on retrofit?

As you probably guessed, the customization of network connection timeouts on Retrofit can't be done directly. Instead, you have to configure OkHttp, Retrofit's network layer, to customize the connection timeouts: OkHttpClient okHttpClient = new OkHttpClient.

What is the default read timeout used by retrofit library?

By default, Retrofit uses the following timeouts: Connection timeout: 10 seconds. Read timeout: 10 seconds. Write timeout: 10 seconds.

What is connect timeout?

A server connection timeout means that a server is taking too long to reply to a data request made from another device. Timeouts are not a reply message: they show up when there isn't a reply and a server request is not fulfilled in a predetermined length of time.


1 Answers

Did you try setting the .writeTimeout on the client:

.writeTimeout(50, TimeUnit.SECONDS)

Also while testing you can pass the timeout duration to 0 if you want the request to not timeout.

And also, the request would fail if there is any connectivity problem or server is not available. Try sending the same request using Postman.

like image 134
jayeshsolanki93 Avatar answered Sep 28 '22 08:09

jayeshsolanki93