Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase timeout for retrofit requests in robospice android?

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;
    }
}
like image 843
100rbh Avatar asked Jul 10 '14 05:07

100rbh


People also ask

What is call in retrofit?

An invocation of a Retrofit method that sends a request to a webserver and returns a response. Each call yields its own HTTP request and response pair. Use clone() to make multiple calls with the same parameters to the same webserver; this may be used to implement polling or to retry a failed call.

What is connection timeout in retrofit?

Connection timeout is the time that start from sending the request to a completed TCP handshake with the server. If Retrofit couldn’t establish the connection to the server within the set connection timeout limit, request is considered as failed. A connection timeout may be set large for countries with bad Internet connection.

What is retrofit for Android?

Get Our Retrofit Book! All modern Android apps need to do network requests. Retrofit offers you an extremely convenient way of creating and managing network requests. From asynchronous execution on a background thread, to automatic conversion of server responses to Java objects, Retrofit does almost everything for you.

What does it mean when a retrofit request is failed?

In other words, if Retrofit couldn't establish a connection to the server within the set connection timeout limit, it'll count the request as failed. For example, increasing the default timeout of ten seconds to a higher value could make sense if you know your users are in conditions with bad Internet connection.

How do I set timeouts on the underlying HTTP client?

Get errorbody response . We can set timeouts settings on the underlying HTTP client. If we don’t specify a client, Retrofit will create one with default connect and read timeouts. By default, Retrofit uses the following timeouts: To customize the timeouts settings you need to configure OkHttp, Retrofit’s network layer. See the code below.


2 Answers

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"
    ...
}
like image 103
gMale Avatar answered Oct 20 '22 00:10

gMale


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

like image 45
Hassan Ibraheem Avatar answered Oct 19 '22 23:10

Hassan Ibraheem