Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguish between writeTimeout,readTimeout and connectTimeout in Retrofit?

I want to post some json string to some URL. I am getting Exception as

 Exception: java.net.SocketTimeoutException: failed to connect to 

searched lot regarding this issue people are suggesting to increase and decrease timeout parameters in retrofit. I want to know difference between writeTimeout, readTimeout and connectTimeout. So that i can trigger SocketTimeoutException. here my retro client.

    public static Retrofit getClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().writeTimeout(20, TimeUnit.SECONDS).
//.authenticator(new Authen()).

        readTimeout(30, TimeUnit.SECONDS).
                        connectTimeout(20, TimeUnit.SECONDS).
                        addInterceptor(interceptor).build();

        return new Retrofit.Builder()
                .baseUrl(DefinesClass.ITS_URL)
//                .baseUrl("https://reqres.in")
//                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(SimpleXmlConverterFactory.create())
                .client(client)
                .build();


    }

Is there any way i can sort that exception help guys?

like image 488
Gaju Kollur Avatar asked Nov 12 '18 07:11

Gaju Kollur


2 Answers

SocketTimeOut meaning your client is not able to reach the server. Try to test the WebService in Postman.

  • The connect timeout is the time-out applied to create a TCP connection to the HTTP server. If the TCP handshake is not completed in this time, the connection attempt failed.
  • The read timeout is the time-out applied from the moment you have established a connection (So handshaking is done, and the connection can be used). If no data comes from the server in this time-out time, the connection is terminated.

same for write timeout we failed to write anything in give time.

like image 124
Jarvis Avatar answered Sep 22 '22 12:09

Jarvis


Difference between all three methods are as below :

connectTimeout :

Sets the default connect timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

The connectTimeout is applied when connecting a TCP socket to the target host. The default value is 10 seconds.

readTimeout :

Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

The read timeout is applied to both the TCP socket and for individual read IO operations including on Source of the Response. The default value is 10 seconds.

writeTimeout :

Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

The write timeout is applied for individual write IO operations. The default value is 10 seconds.


Source from here.

like image 42
Jeel Vankhede Avatar answered Sep 21 '22 12:09

Jeel Vankhede