0

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.

Geo ZiDani
  • 111
  • 1
  • 1
  • 8

2 Answers2

0

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;
        }
    };
}

}

mykolaj
  • 974
  • 8
  • 17
  • OK I will try with this values. I changed RetryPolicy values by implementing my custom RetryPolicy but always I made more then one request and after mainipulating delay flag I get always te same result. It looks like request was timeouted after 30 second and next request sent after i.e 120 sec (when flag was set on 120L) – Geo ZiDani Apr 14 '16 at 21:50
  • It looks like 30 second timeout happens on a server side. I suspect that in the first case it was 30 seconds of networking + 2.5 seconds of Robospice's default retry timeout. In the second case it should be 30 seconds of networking + 120 seconds of RoboSpice's retry timeout. Try to use Wireshark to see whats really happening with networking. It looks suspicious that timeouts happen so frequently. – mykolaj Apr 15 '16 at 20:30
  • After couple of days I back to this issue and I have some remarks. First of all your approach resolve problem but not in 100%. It increase time of request but in form of delay. I see that server which I use send me timeout after 10 sec and after this robospice wait DELAY TIME set in CustomRetryPolicy. It is OK right now because sever also cached my request and if response is ready it send me in next request. – Geo ZiDani May 05 '16 at 11:20
  • Other problem is with "retry count". If I implement CustomRetryPolicy value of retry count is ignored and if server send sockettimeout, robospice send request after request on and on. If I used default retry policy only 3 request are send and exception is catched. Also after implementing CustomRetryPolicy a canceling running request is not working as with default one. I would mention that I used OkHttp3 in version 3.20 – Geo ZiDani May 05 '16 at 11:32
0

The answer provided by av_lee is right, but the method of defining a Custom Retry Policy for RoboSpice is not correct. The above mentioned code will result in infinite retries of call in case of failure. RoboSpice will always use value returned by getRetryCount() and will decrement it after retrying, but next time it will again get value 1 and so on. The correct way to implement Custom Retry Policy is to set values of DefaultRetryPolicy class.

public class CustomRetryPolicy extends DefaultRetryPolicy {
  public CustomRetryPolicy(int retryCount) {
      super(retryCount, DEFAULT_DELAY_BEFORE_RETRY, DEFAULT_BACKOFF_MULT);
  } 
}

In this above code, retryCount is the number of retries you want for your request and default values for delay before retry and backoff multiplier are used. You can use your preferred values for any of these three.

Lala Rukh
  • 373
  • 1
  • 3
  • 4