手柄连接和读取超时android中的RestClient电话

手柄连接和读取超时android中的RestClient电话

本文介绍了手柄连接和读取超时android中的RestClient电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我现在用我的整个应用程序许多休息调用的RestService接口。

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

我设置超时处理连接读超时

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..!?

推荐答案

超时,坏的网关,主机没有发现和其他的插槽例外不能覆盖ErrorHandlers。 ErrorHandlers的目标是寻找错误的作为ResponseErrorHandler的方法签名表示现有的响应。

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.

所有插座异常抛出RestClientException,必须抓住每一个RestTemplate操作,例如getForObject()在try ... catch块。

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
}

查看引用。

希望,帮助。

这篇关于手柄连接和读取超时android中的RestClient电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:43