2013-05-14 1 views
4

응용 프로그램을 통해 사용하고있는 Rest를 많이 사용하는 RestService 인터페이스가 있습니다.android의 RestClient 호출에 대한 연결 및 읽기 제한 처리

나는 connection

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); 
     } 
    } 

read-timeouts을 처리하기위한 시간 제한을 설정하고하지만 시간 초과 상황을 처리 붙어 있어요. 이 메서드를 사용하여 생각했지만 rest 호출이 실패 할 때이 루프에 들어오지 않습니다.

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())); 
     } 
    }); 

아무도 도와 줄 수 없다 ..!?

답변

7

시간 초과, 잘못된 게이트웨이, 호스트를 찾을 수 없음 기타 소켓 예외는 ErrorHandlers에서 처리 할 수 ​​없습니다. ErrorHandler의 대상은 ResponseErrorHandler의 메서드 시그니처에 설명 된대로 기존 Response의 오류를 찾는 것입니다.

모든 소켓 예외는 RestClientException을 발생시키고 try ... catch 블록에서 getForObject()와 같은 모든 RestTemplate 작업에 대해 catch해야합니다.

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

체크 아웃 reference.

희망, 도움이됩니다.

+0

감사의 말과 제안에 대해 @alexei에게 감사드립니다. 그 태그를 추가했습니다. 질문에 관해서는, 내가 설명한 것처럼 REstClientException을 처리하고 있습니다. 내가 필요한 것은 처리기 내부에서 Exception을 가져 와서 처리하는 것입니다. SocketTimeoutExceptions의 경우 메시지로 클라이언트에서 정상적으로 처리하려고합니다. 나는이 답변에서 같은 코멘트 : http://stackoverflow.com/a/14320675/1237656. 한번 봐주세요..! – DroidBee

+1

글쎄,이 문제에 대한 요청을 발견했지만 정당한 응답이 없습니다. http://forum.springsource.org/showthread.php?98449-Socket-read-timeout-exception-handling-with-http-gateway –

관련 문제