Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle Connection and Read Timeouts for RestClient calls in android

I have a RestService interface with many rest calls which I am using throughout my application.

I am setting timeouts for handling connection and read-timeouts

ClientHttpRequestFactory httpFactory = myRestService.getRestTemplate().getRequestFactory();
    if(httpFactory!=null)
    {
        if(httpFactory instanceof SimpleClientHttpRequestFactory)
        {
            ((SimpleClientHttpRequestFactory)httpFactory).setConnectTimeout(10*1000);
            ((SimpleClientHttpRequestFactory)httpFactory).setReadTimeout(30*1000);
        }
        else if(httpFactory instanceof HttpComponentsClientHttpRequestFactory)
        {
            ((HttpComponentsClientHttpRequestFactory)httpFactory).setConnectTimeout(10*1000);
            ((HttpComponentsClientHttpRequestFactory)httpFactory).setReadTimeout(30*1000);
        }
    }

But I am stuck with handling the timeout situation. I thought of using this method but it is not coming into this loop when rest call fails.

myRestService.getRestTemplate().setErrorHandler(new ResponseErrorHandler() 
    {
        @Override
        public boolean hasError(ClientHttpResponse paramClientHttpResponse) throws IOException 
        {
            Log.e(TAG, paramClientHttpResponse==null?"Null response" : ("Has Error : " + paramClientHttpResponse.getStatusText()+" , status code : "+paramClientHttpResponse.getStatusCode()));

            return false;
        }
        @Override
        public void handleError(ClientHttpResponse paramClientHttpResponse) throws IOException 
        {
            Log.e(TAG, paramClientHttpResponse==null?"Null response":("Handle Error : " + paramClientHttpResponse.getStatusText()+" , status code : "+paramClientHttpResponse.getStatusCode()));
        }
    });

Can anybody help me with this..!?

like image 301
Aswathy P Krishnan Avatar asked May 14 '13 12:05

Aswathy P Krishnan


People also ask

What is the difference between connection timeout and read timeout?

The connection timeout is the timeout in making the initial connection; i.e. completing the TCP connection handshake. The read timeout is the timeout on waiting to read data1.

What is connection timeout and read time RestTemplate?

Connection timeout is used when opening a communications link to the remote resource. A java.net.SocketTimeoutException is thrown if the timeout expires before the connection can be established. Read timeout is used when reading from Input Stream when a connection is established to a remote resource.

What is the default connection timeout for RestTemplate?

The default timeout is infinite. By default RestTemplate uses SimpleClientHttpRequestFactory and that in turn uses HttpURLConnection.


1 Answers

Timeout, bad gateway, host not found and other socket exceptions can not be covered by ErrorHandlers. The target of ErrorHandlers is to look for the errors in an existing Response as stated in the ResponseErrorHandler's method signature.

All socket exceptions throw RestClientException and must be caught for every RestTemplate operation such as getForObject() in try...catch block.

try {
    repr = myRestService.getRestTemplate().getForObject(url, responseType, vars);
} catch (RestClientException e) {
    //Further exception processing, forming negative response should be here
}

Check out reference.

Hope, that helps.

like image 106
alexei burmistrov Avatar answered Oct 05 '22 23:10

alexei burmistrov