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).
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.
By default, Retrofit uses the following timeouts: Connection timeout: 10 seconds. Read timeout: 10 seconds. Write timeout: 10 seconds.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With