Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to config the Timeout #641

Closed
ghost opened this issue Oct 26, 2014 · 3 comments
Closed

How to config the Timeout #641

ghost opened this issue Oct 26, 2014 · 3 comments

Comments

@ghost
Copy link

ghost commented Oct 26, 2014

Hi, I have a problem on retrofit about to config the timeout with retrofit. My code like this:
sBaseUrl = Configs.getString(Configs.BASE_URL);

    RestAdapter.Builder builder = new RestAdapter.Builder()
            .setEndpoint(sBaseUrl)
            .setConverter(new JacksonConverter())
            .setRequestInterceptor(
                    new ApiAuthInterceptor(MyApplication.getInstance()))
            .setErrorHandler(new ApiErrorHandler());

    builder.setLogLevel(LogLevel.FULL).setLog(new Log() {
        @Override
        public void log(String arg) {
            android.util.Log.d("rest", arg);
        }
    });

how to do it?? Is there a way to config the timeout no matter which kind of way to send a request such as okhttp or other??

@JakeWharton
Copy link
Member

Retrofit does not support setting timeouts for two reasons:

  1. It's not a uniform setting. Libraries like OkHttp allow setting globally but HttpUrlConnection does not.
  2. Retrofit wraps an HTTP client and doesn't set any other properties you might also want on it. It's not going to get in the business of doing this either.

If you are using OkHttp you can set timeouts on the OkHttpClient and then pass it to the RestAdapter.Builder:

builder.setClient(new OkClient(client))

For the JDK's HttpUrlConnection you'll need to set the properties on each request which means subclassing UrlConnectionClient:

public final class MyUrlConnectionClient extends UrlConnectionClient {
  @Override protected HttpUrlConnection openConnection(Request request) {
    HttpUrlConnection connection = super.openConnection(request);
    connection.setConnectTimeout(15 * 1000);
    connection.setReadTimeout(30 * 1000);
    return connection;
  }
}

and then passing an instance to the RestAdapter.Builder:

builder.setClient(new MyUrlConnectionClient());

@rpalsaxena
Copy link

I am getting timeout error in logcat when i used retofit on my code... Could you please suggest me a solution.

@cassianomenezes
Copy link

Hey, @JakeWharton! I'm facing some timeout issues in my application.
I followed the documentation and examples online. I use Retrofit to make this REST call:
@ GET("/resolver/resolve")
public ResolverResult resolve(@ Query("id") String id);

My OkClient object is set like this:

OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
ArrayList protocols = new ArrayList();
protocols.add(Protocol.HTTP_1_1);
client.setProtocols(protocols);
client.setFollowSslRedirects(true);
client.setFollowRedirects(true);

In my Builder I'm setting the client as this okClient I just described (it works fine).

My idea is to catch timeout situations/exceptions when they happen. I want to show my user the timeout happened and he/she could try again later.
To achieve this, I'm simulating very bad internet connection cases but Retrofit doesn't seem to follow the timeout values I've set. It just keeps trying again and again.

Am I missing something? Is there any other documentation I can follow to fix this?

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants