美文网首页
Android 响应数据封装

Android 响应数据封装

作者: Il_mondo | 来源:发表于2018-07-14 11:47 被阅读15次
    public abstract class BaseResponseModel<T> {
    
        /**
         * 响应码
         */
        static final int ERROR_CODE = 0;
        static final int SUCCEED_CODE = 1;
        static final int TOKEN_ERROR = -1;
    
        private T data;
    
        private int code;
    
        private String msg;
    
    
        public BaseResponseModel() {
            // 反射,生成对象用
        }
    
        private BaseResponseModel(String msg, int code) {
            this.msg = msg;
            this.code = code;
        }
    
        public <E extends BaseResponseModel> E error() {
            code = ERROR_CODE;
            msg = "网络错误";
            return (E) this;
        }
    
        public boolean isSucceed() {
            return code == SUCCEED_CODE;
        }
    
        /**
         * 是否重复登录
         */
        public boolean isRepeatLogin(){
            return code == TOKEN_ERROR;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public int getCode() {
            return code;
        }
    
        public void setCode(int code) {
            this.code = code;
        }
    
        public T getData() {
            return data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    }
    
    public class LoginResponseModel extends BaseResponseModel<LoginResponseModel.Info>{
    
        /**
         * 具体返回的信息
         */
        public static class Info{
    
        }
    }
    
    public interface DataSource {
    
        interface Callback<T>{
            void onResult(T response);
        }
    
        /**
         * 用户登录
         */
        void login(String uid, String token, Callback<LoginResponseModel> callback);
    }
    
    // -----------------------------------
    // 生成错误消息,
    public class RemoteDataSource implements DataSource {
    
        @Override
        public void login(String uid, String token, Callback<LoginResponseModel> callback) {
            callback.onResult(new LoginResponseModel().error());
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Android 响应数据封装

          本文链接:https://www.haomeiwen.com/subject/zxzepftx.html