Announcement Announcement Module
No announcement yet.
How can I handle GET requests that are taking to long? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How can I handle GET requests that are taking to long?

    Hey folks,

    I read a tutorial here. This raised the question how I could handle a request that takes to long (also asked here).

    MyCode looks something like this (c&p from the tutorial)
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate();
    // The URL for making the GET request
    final String url = "";
    // Instantiate the HTTP GET request, expecting an array of 
    // Product objects in response
    Product[] products = restTemplate.getForObject(url, Product[].class);

    So please tell me: whats the best practice?

  • #2
    Both the standard J2SE Http client and HttpComponents client allow you to set a timeout. Within Spring for Android this is exposed via a couple methods in the respective request factories, setConnectTimeout and setReadTimeout.

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    There is also a full example in the samples repository.

    Hope that helps!