Hello,
I've been looking a way to handle timeouts with the Rest template.
What I did, was the following:
ClientHttpRequestFactory factory = tpl.getRequestFactory();
if ( factory instanceof SimpleClientHttpRequestFactory)
{
((SimpleClientHttpRequestFactory) factory).setConnectTimeout( 5 * 1000 );
((SimpleClientHttpRequestFactory) factory).setReadTimeout( 5 * 1000 );
}
else if ( factory instanceof HttpComponentsClientHttpRequestFactory)
{
((HttpComponentsClientHttpRequestFactory) factory).setReadTimeout( 5 * 1000);
((HttpComponentsClientHttpRequestFactory) factory).setConnectTimeout( 5 * 1000);
}
tpl.setRequestFactory( factory );
However, I'm not sure what is triggered or where can I catch a timeout and do something with it.
My implementation, ATM is with an interceptor (https://github.com/excilys/androidannotations/wiki/Rest%20API#interceptors). There it checks if device has internet connection, caches the response, etc. so it would be great to implement something to handle the TimeOut there aswell.
As an example, this is what is happening when my device has connection and everything works flawlessly:
SLog.d( "Before" );
ClientHttpResponse response = requestExecution.execute( request, bytes );
SLog.d( "After" );
And the log output is the following:
01-30 07:49:50.896 1308-1370/com.example. D/SLog﹕ Before
01-30 07:49:51.904 1308-1370/com.example D/SLog﹕ After
But if I set an unexisting gateway, so the request gets lost, the "After" text is never shown.
What I was trying to do, is to check the Headers from response object generated by the "execute" method of RequestExecution. And check if the response was a TIMEOUT. I can handle 500 responses, for example, but never TIMEOUT responses.
What I was thinking about, is that maybe ClientHttpResponse object generated by "execute" method, is always filled up with information from server and is never filled with information if the server doesn't return any information / can't reach server.
Any tip about how / where can I implement code to handle timeout? Note that if it's in the interceptor, it would be so great.
Thanks.
Hello,
I've been looking a way to handle timeouts with the Rest template.
What I did, was the following:
However, I'm not sure what is triggered or where can I catch a timeout and do something with it.
My implementation, ATM is with an interceptor (https://github.com/excilys/androidannotations/wiki/Rest%20API#interceptors). There it checks if device has internet connection, caches the response, etc. so it would be great to implement something to handle the TimeOut there aswell.
As an example, this is what is happening when my device has connection and everything works flawlessly:
And the log output is the following:
01-30 07:49:50.896 1308-1370/com.example. D/SLog﹕ Before
01-30 07:49:51.904 1308-1370/com.example D/SLog﹕ After
But if I set an unexisting gateway, so the request gets lost, the "After" text is never shown.
What I was trying to do, is to check the Headers from response object generated by the "execute" method of RequestExecution. And check if the response was a TIMEOUT. I can handle 500 responses, for example, but never TIMEOUT responses.
What I was thinking about, is that maybe ClientHttpResponse object generated by "execute" method, is always filled up with information from server and is never filled with information if the server doesn't return any information / can't reach server.
Any tip about how / where can I implement code to handle timeout? Note that if it's in the interceptor, it would be so great.
Thanks.