2014-10-27 74 views
1

我使用'Retrofit'進行http轉移。改造:自定義ErrorHandler,UndeclaredThrowableException

我做了一個自定義ErrorHandler像下面的代碼。

public class CustomErrorHandler implements ErrorHandler { 

    @Override 
    public Throwable handleError(RetrofitError cause) { 

     if (RetrofitError.Kind.NETWORK == cause.getKind()) { 

      Throwable throwable = cause.getCause(); 

      if (throwable instanceof SocketTimeoutException) 
       return throwable; 
      else if (throwable instanceof ConnectException) 
       return throwable; 
     } 

     return cause; 
    } 

} 

當我嘗試調試,throwable是在CustomErrorHandler.handleError()SocketTimeoutExceptionConnectException實例。

但是,下面的提示UndeclaredThrowableException

private SignOutSuccessResponse trySignOut(String email, String userToken) throws Exception { 

    RestAdapter.Builder builder = new RestAdapter.Builder(); 
    builder.setEndpoint(ServerInfo.END_POINT_API); 
    builder.setErrorHandler(new CustomErrorHandler()); 

    if (BuildConfig.DEBUG) 
     builder.setLogLevel(RestAdapter.LogLevel.FULL); 

    API sessionAPI = builder.build().create(API.class); 
    return sessionAPI.signOut(email, userToken); 
} 

如何解決此問題而不是this way

我使用

  • 的Android工作室
  • 改造1.7.1
  • OkHttp 2.0.0

請幫我ㅠㅠ

回答

0

兩個SocketTimeExceptionConnectException檢查例外,因爲你可能已經知道。如果你不想用你發佈的鏈接中規定的檢查異常聲明你的接口,那麼你需要在返回它們之前將這兩個異常包裝成一個未經檢查的異常。

+0

哦,我不知道'SocketTimeException'和'ConnectException'是'checked exception'。謝謝! – 2014-10-27 13:38:45