androidokhttprobospicegoogle-http-client

Increase time of invoking loadDataFromNetwork() of RoboSpice service


I use RoboSpice with OkHttpClient module (OkHttpSpiceService) for quite long time requests. For that purposes I need to increase timeouts of http client so I made and set them on 120 seconds.

    @Override
    protected OkHttpClient createOkHttpClient() {
            OkHttpClient okHttpClient = super.createOkHttpClient();
            okHttpClient.setConnectTimeout(120, TimeUnit.SECONDS);
            okHttpClient.setReadTimeout(120, TimeUnit.SECONDS);
            okHttpClient.setWriteTimeout(120, TimeUnit.SECONDS);
            return okHttpClient;
}

I do not use caching option so I call SpiceRequest by

getSpiceManager().execute(spiceRequest, this);

After this SpiceService invoking loadDataFromNetwork() every 30 seconds (3 times) when response is not comming or is not reachable in this short time.

Is any posibilites to increase or change of time of invoking loadDataFromNetwork()? I know that I get response after one minute but using this methods I cannot reach proper response.


Solution

  • By default RoboSpice uses a DefaultRetryPolicy like this:

    /** The default number of retry attempts.*/    
    public static final int DEFAULT_RETRY_COUNT = 3;
    
    /** The default delay before retry a request (in ms). */
    public static final long DEFAULT_DELAY_BEFORE_RETRY = 2500;
    

    What you can do is to implement your own retry policy by extending DefaultRetryPolicy class, and by overriding this two methods:

    public class CustomRetryPolicy extends DefaultRetryPolicy {
        @Override
        public int getRetryCount() { return 1; }
    
        @Override
        public long getDelayBeforeRetry() { return 120L * 1000; }
    }
    

    Than you can use your custom retry policy like this:

    spiceRequest.setRetryPolicy(new CustomRetryPolicy());
    

    Take a look here: https://github.com/stephanenicolas/robospice/wiki/Advanced-RoboSpice-Usages-and-FAQ#how-can-i-setup-a-retry-policy-for-failed-requests-

    I do not use caching option so I call SpiceRequest by getSpiceManager().execute(spiceRequest, this);

    By the way, this does not stop RoboSpice from using of cache. To really stop SpiceService from using the cache you need to override createCacheManager method in your own OkHttpSpiceService implementation like this:

    public class MyOkHttpSpiceService extends OkHttpSpiceService {
    
    @Override
    public CacheManager createCacheManager(Application application) {
        // Just return an empty CacheManager
        return new CacheManager() {
            @Override
            public <T> T saveDataToCacheAndReturnData(T data, Object cacheKey) throws CacheSavingException, CacheCreationException {
                return data;
            }
        };
    }
    

    }