Retrofit: config special interface connection timeout with okhttp?

Created on 3 Jun 2016  Â·  4Comments  Â·  Source: square/retrofit

how to config special interface connection timeout with okhttp?
I have some special interfaces which expect connection timeout is short e.g. 2s . and other interfaces should be long e.g. 10s, how to config the special one with okhttp via retorfit without defind another OkHttpClient?

Most helpful comment

Share common configuration in the builders and then customize each to suit.

OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder() // + common setup...
Retrofit.Builder retrofitBuilder = new Retrofit.Builder() // + common setup...

OkHttpClient shortHttpClient = httpBuilder.readTimeout(2, SECONDS)
    .writeTimeout(2, SECONDS)
    .connectTimeout(2, SECONDS)
    .build();
Retrofit shortRetrofit = retrofitBuilder.client(shortHttpClient).build();
ShortInterface short = shortRetrofit.create(ShortInterface.class);

OkHttpClient longHttpClient = httpBuilder.readTimeout(15, SECONDS)
    .writeTimeout(15, SECONDS)
    .connectTimeout(15, SECONDS)
    .build();
Retrofit longRetrofit = retrofitBuilder.client(longHttpClient).build();
LongInterface long = longRetrofit.create(LongInterface.class);

We'll eventually support Retrofit-level interceptors which will allow changing timeouts using things like annotations per-interface or per-endpoint ( #1732 ).

All 4 comments

Share common configuration in the builders and then customize each to suit.

OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder() // + common setup...
Retrofit.Builder retrofitBuilder = new Retrofit.Builder() // + common setup...

OkHttpClient shortHttpClient = httpBuilder.readTimeout(2, SECONDS)
    .writeTimeout(2, SECONDS)
    .connectTimeout(2, SECONDS)
    .build();
Retrofit shortRetrofit = retrofitBuilder.client(shortHttpClient).build();
ShortInterface short = shortRetrofit.create(ShortInterface.class);

OkHttpClient longHttpClient = httpBuilder.readTimeout(15, SECONDS)
    .writeTimeout(15, SECONDS)
    .connectTimeout(15, SECONDS)
    .build();
Retrofit longRetrofit = retrofitBuilder.client(longHttpClient).build();
LongInterface long = longRetrofit.create(LongInterface.class);

We'll eventually support Retrofit-level interceptors which will allow changing timeouts using things like annotations per-interface or per-endpoint ( #1732 ).

that was I did rgiht now ,but it will be have two Retrofits instance in memory.

They aren't that expensive such that you need to worry about that. The
generated implementations of interfaces are the expensive bits that you
only want one of each.

On Fri, Jun 3, 2016, 3:29 AM sharyuke [email protected] wrote:

that was I did rgiht now ,but it will be have two Retrofits instance in
memory.

—
You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub
https://github.com/square/retrofit/issues/1836#issuecomment-223510333,
or mute the thread
https://github.com/notifications/unsubscribe/AAEEEco87LA2vcmxU2O54VzALzikTfzaks5qH9fYgaJpZM4ItQnp
.

@JakeWharton ok got that ,thanks ,but i hope retrofit can set special connection timeout to each of interface, like @POST ,@GET @HEADERS ... annotations . by the way . I hope retrofit can set common params to every interfaces created by public <T> T create(final Class<T> service) { ,so that i can defind less params to every method.

Was this page helpful?
0 / 5 - 0 ratings