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());
}
}
网友评论