5

I am using RoboSpice for Rest Api calls in android and i want to add connection timeout for 30 secs in calls how i will do ?

here is my code

 public class AddBrandsService extends
        SpringAndroidSpiceRequest<AddBrands.Response> {

     public final AddBrands.Response loadDataFromNetwork(){

     return getRestTemplate().postForObject(url,
            request, AddBrands.Response.class);
    }

    }


    this service is called here 

    private SpiceManager contentManager = new SpiceManager(
        JacksonSpringAndroidSpiceService.class);

    contentManager.execute(service, lastRequestCacheKey,
                DurationInMillis.ONE_SECOND, new AddBrandsListner());

thanks in advance...

2
  • If you post your SpiceService it would be easier to answer you.
    – rciovati
    May 27, 2013 at 8:32
  • It all depends on the module you are using. I can help for sure with SpringAndroid. Ping me when you update your answer @Zaid Bin Tariq
    – Snicolas
    May 27, 2013 at 18:26

1 Answer 1

3

Here is the code. Basically, you have to take care of the version of android as spring android switch between two different implementations to avoid a known bug in network stack. Unfortunately both implementations don't share a common interface whith respect to timeouts.

private void manageTimeOuts(RestTemplate restTemplate) {
    // set timeout for requests
    ClientHttpRequestFactory factory = restTemplate.getRequestFactory();
    if (factory instanceof HttpComponentsClientHttpRequestFactory) {
        HttpComponentsClientHttpRequestFactory advancedFactory = (HttpComponentsClientHttpRequestFactory) factory;
        advancedFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
        advancedFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
    } else if (factory instanceof SimpleClientHttpRequestFactory) {
        SimpleClientHttpRequestFactory advancedFactory = (SimpleClientHttpRequestFactory) factory;
        advancedFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
        advancedFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
    }
}
1
  • The timeout is there to set a limit on how long the wait is for incoming data. A SocketTimeoutException can be thrown when reading from the returned input stream if the read timeout expires before data is available for read.
    – DMonkey
    Jul 17, 2017 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.