Announcement Announcement Module
Collapse
No announcement yet.
How can I handle GET requests that are taking to long? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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)
    Code:
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate();
    
    // The URL for making the GET request
    final String url = "http://10.0.2.2:8080/rest/products";
    
    // 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.

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

    Hope that helps!

    Comment

    Working...
    X