我用“Retrofit”进行http传输。
我做了一个定制的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是SocketTimeoutException或ConnectException在CustomErrorHandler.handleError()中的实例。
但是,下面的函数抛出了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
我在用
请帮我ㅠㅠ
发布于 2014-10-27 12:27:38
正如您可能已经知道的,SocketTimeException和ConnectException都是选中的异常。如果您不想像您发布的链接中所规定的那样使用检查异常声明您的接口,那么在返回它们之前,您需要将两个异常包装成一个未检查的异常。
https://stackoverflow.com/questions/26582878
复制相似问题