How to increase timeout for retrofit requests in robospice android?
Asked Answered
A

3

13

I have implemented robospice in my project and using retrofit for all api calls. For some of requests, I need to increase timeout, please let me know how can I do that?

Actually I am using service that extends RetrofitGsonSpiceService. The code of my service class is below:

public class MyService extends RetrofitGsonSpiceService {

    @Override
    public void onCreate() {
        super.onCreate();
        addRetrofitInterface(MyInterface.class);
    }

    @Override
    public CacheManager createCacheManager(Application application) throws CacheCreationException {
        CacheManager cacheManager = new CacheManager();
        ObjectPersisterFactory persistFactory = new RetrofitObjectPersisterFactory(application,
                getConverter(), getCacheFolder());

        persistFactory.setAsyncSaveEnabled(true);
        cacheManager.addPersister(persistFactory);

        return cacheManager;
    }

    @Override
    protected String getServerUrl() {
        return Utils.getBaseUrl();
    }

    @Override
    protected RestAdapter.Builder createRestAdapterBuilder() {
        RestAdapter.Builder builder = new RestAdapter.Builder().setRequestInterceptor(
                new RequestInterceptor() {
                    @Override
                    public void intercept(RequestFacade request) {
                        request.addHeader(Const.HEADER_DEVICE_TYPE_KEY,
                                Const.HEADER_DEVICE_TYPE_VALUE);
                    }
                }
        );
        builder.setEndpoint(getServerUrl()).setConverter(getConverter());
        return builder;
    }
}
Abrasion answered 10/7, 2014 at 5:50 Comment(0)
B
17

I stumbled in here with a similar question and eventually found the answer elsewhere. Had there been a more complete example here, I would have saved some time so I circled back to post what worked for me just in case it helps others:

Adapter with increased read timeout

    // create client
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setReadTimeout(60 * 1000, TimeUnit.MILLISECONDS);

    // create rest adapter using the client above
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(getBaseApiUrl())
            .setClient(new OkClient(okHttpClient))
            // this gson converter below is optional. We use it for parsing dates and enums
            .setConverter(new GsonConverter(createApiGson()))
            .setLogLevel(getRetrofitLogLevel())
            .build();

NOTE: for us, getBaseApiUrl() returns something like: https://some.company.com/api/
and getRetrofitLogLevel() returns either RestAdapter.LogLevel.FULL or RestAdapter.LogLevel.NONE depending on which flavor of the app is built.

Lastly, these are the main dependencies that make everything work:

Key dependencies

dependencies {
    ...
    compile "com.squareup.retrofit:retrofit:1.5.0"
    compile "com.squareup.retrofit:retrofit-mock:1.5.0"
    compile "com.squareup.okhttp:okhttp:1.5.4"
    compile "com.google.code.gson:gson:2.2.4"
    ...
}
Brigadier answered 4/11, 2014 at 3:17 Comment(0)
B
6

In your RetrofitSpiceService extending service, you'll need to override createRestAdapterBuilder().

 @Override
 protected Builder createRestAdapterBuilder() {
    Builder builder = super.createRestAdapterBuilder();
    builder.setClient(new CustomClient());
    return builder;
  }

CustomClient is the class that you need to write yourself to supply your own timeouts. If you're using OkHttp, take a look at the default OkClient for reference: https://github.com/square/retrofit/blob/43b7ea14e5aca1d710deccb95b79484b03e99bb9/retrofit/src/main/java/retrofit/client/OkClient.java

Barbitone answered 15/7, 2014 at 9:53 Comment(0)
A
3

I don't know about robospice but with retrofit 2+ and okhttp3+ you should set a new client like this.

OkHttpClient okHttpClient = new OkHttpClient();
Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constantes.URL_DOMAIN)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient.newBuilder().connectTimeout(Constantes.TIMEOUT, TimeUnit.SECONDS).readTimeout(Constantes.TIMEOUT, TimeUnit.SECONDS).writeTimeout(Constantes.TIMEOUT, TimeUnit.SECONDS).build())
            .build();

import this in your module build.gradle:

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
Apocrine answered 2/2, 2016 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.