Setting connection time In RoboSpice android request

I use RoboSpice calls to rest Api in android and I want to add a connection timeout for 30 seconds in calls, how will I 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...

+4
source share
1 answer

Here is the code. Basically, you should take care of the android version as spring android switch between two different implementations in order to avoid a known error in the network stack. Unfortunately, both implementations do not have a common interface, taking into account 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); } } 
+3
source

Source: https://habr.com/ru/post/1482321/


All Articles