Catch http response in Retrofit before passing it to the calling activity

后端 未结 2 649
南旧
南旧 2020-12-30 12:15

Right now we are using retrofit like this:

service.executeSomeRequest(UserPreferenceRequest userPreferenceRequest, new Callback         


        
2条回答
  •  执笔经年
    2020-12-30 12:24

    Your custom callback can process the response in the base class first and then delegate to an abstract method.

    public interface StatusResponse {
      Status getStatus();
    }
    
    public abstract class CustomCallback implements Callback {
      @Override public final void success(T data, Response response) {
        if (data.getStatus() == Status.OK) {
          success(data);
        } else {
          // Handle error..
        }
      }
    
      public abstract void success(T data);
    }
    

提交回复
热议问题