/**
* 用于网络请求结果的回调的抽象类,使用该类可以保证网络请求在主线程发起,在子线程回调
* 使用的异步任务,并且保证了异步任务的创建在主线程执行
*/
public abstract class Callback<T> {
private AsyncTask<Void, Void, Void> asyncTask;
private T obj;
private int code;
private boolean status;
public Callback() {
asyncTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void v) {
if (status)
Callback.this.onSuccess(obj);
else
Callback.this.onFailure(code);
}
};
}
public void notifyUI(T obj, int code, boolean status) {
this.obj = obj;
this.code = code;
this.status = status;
this.asyncTask.execute();
}
protected abstract void onSuccess(T obj);
protected abstract void onFailure(int code);
}
一个小朋友写的。