自定义Subscriber

/**

* 自定义Subscriber,将dialog、complete、error、错误返回抽取出来,实现类中只进行Success的处理

*/

public abstract class MSubscriber extends Subscriber> {

privateLoadingDialogdialog;

private booleanflag;

publicMSubscriber() {

}

publicMSubscriber(Activitycontext) {

if(context !=null) {

dialog=newLoadingDialog(context);

dialog.setText("正在访问网络,请稍后...");

dialog.showDialog();

}

}

publicMSubscriber(Activitycontext,Stringmsg) {

if(context !=null) {

dialog=newLoadingDialog(context);

dialog.setText(msg);

dialog.showDialog();

}

}

publicMSubscriber(Activitycontext, booleanflag) {

if(context !=null) {

dialog=newLoadingDialog(context);

dialog.setText("正在访问网络,请稍后...");

dialog.showDialog();

}

this.flag= flag;

}

@Override

public voidonCompleted() {

}

@Override

public voidonError(Throwablee) {

if(dialog!=null) {

dialog.dismissDialog();

}

e.printStackTrace();

doError(e);

//ToastUtils.makeText("网络请求失败");

}

//选择性重写,处理error

protected voiddoError(Throwablee) {

}

@Override

public voidonNext(DataResponse data) {

if(dialog!=null) {

dialog.dismissDialog();

}

if("0".equals(data.status)) {

doNext(data.data);

}else{

doOther(data);

}

if(flag) {

ToastUtils.makeText(data.message);

}

}

//选择性重写,处理网络访问成功,但是不是正确的返回结果

protected voiddoOther(DataResponse data) {

}

protected abstract voiddoNext(Tdata);

}

你可能感兴趣的:(自定义Subscriber)