我有一个 RestService 接口,其中包含我在整个应用程序中使用的许多休息调用。
我正在设置处理超时connection
和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);
}
}
但我坚持处理超时情况。我想过使用这种方法,但是当休息调用失败时它不会进入这个循环。
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()));
}
});
任何人都可以帮我解决这个问题..!?