public abstract class ProgressObserver implements Observer {
privateBaseActivitymBaseActivity;
private booleanisShowDialog;
publicProgressObserver(BaseActivity baseActivity) {
this(baseActivity, true);
}
publicProgressObserver(BaseActivity baseActivity, booleanisShowDialog) {
if(baseActivity ==null) {
throw newNullPointerException();
}
this.mBaseActivity= baseActivity;
this.isShowDialog= isShowDialog;
}
@Override
public voidonSubscribe(Disposable d) {
mBaseActivity.addSubscription(d);
onStart();
}
@Override
public voidonNext(Tvalue) {
onProgressNext(value);
}
/**
* --> MyGsonResponseBodyConverter中已对返回数据做初步处理
* --> zlc.season.rxjava2demo.retrofit.converter.MyGsonResponseBodyConverter#convert(okhttp3.ResponseBody)
*
*@parameThrowable
*/
@Override
public voidonError(Throwable e) {
if(einstanceofConnectException) {
ToastUtil.show(NetworkUtil.isNetworkConnected(mBaseActivity) ? R.string.net_server_connectexception: R.string.net_connectexception);
}else if(einstanceofUnknownHostException) {
ToastUtil.show(R.string.net_unknownhostexception);
}else if(einstanceofSocketException) {
ToastUtil.show(R.string.net_socketexception);
}else if(einstanceofSocketTimeoutException) {
ToastUtil.show(R.string.net_sockettimeoutexception);
}else if(einstanceofIOException) {
ToastUtil.show(R.string.server_internal_error);
}else if(einstanceofNullPointerException) {
ToastUtil.show(R.string.server_internal_error);
}else if(einstanceofHttpException) {
ToastUtil.show(R.string.server_internal_error);
}else if(einstanceofAppException) {
ToastUtil.showLong(e.getMessage());
AppException exception = (AppException) e;
switch(exception.errorCode) {
caseHttpCode.TOKEN_INVALID:
mBaseActivity.kickOut();
break;
caseHttpCode.BIND_SUCCESS:
ToastUtil.debugShow("手环绑定成功");
break;
caseHttpCode.THIRD_NO_REGISTER:
ToastUtil.debugShow("第三方登录时未注册");
break;
caseHttpCode.THIRD_REPEAT_REGISTER:
ToastUtil.debugShow("第三方登录时已经绑定");
break;
caseHttpCode.PHONE_NUMBER_REPEAT_REGISTER:
ToastUtil.show("手机号码已注册");
break;
caseHttpCode.NO_USER_INFO:
ToastUtil.debugShow("无此手机号的用户信息");
break;
caseHttpCode.BRACELET_INACTIVE:
ToastUtil.debugShow("手环未激活");
break;
caseHttpCode.USER_BIND_BRACELET:
ToastUtil.debugShow("用户已绑定手环");
break;
caseHttpCode.BRACELET_BIND:
ToastUtil.debugShow("手环已绑定");
break;
caseHttpCode.BRACELET_ACTIVE:
ToastUtil.debugShow("手环已激活");
break;
caseHttpCode.CONTACT_EXIST:
ToastUtil.show("列表已有此联系人");
break;
caseHttpCode.USER_NO_BRACELET:
ToastUtil.show("未绑定手环");
break;
caseHttpCode.NO_HISTORICAL_DATA:
ToastUtil.show("无历史数据");
break;
default:
break;
}
}else{
ToastUtil.showLong(e.toString());
}
onProgressError(e);
onFinish();
}
@Override
public voidonComplete() {
onFinish();
}
/**
*开始网络请求.
*/
public voidonStart() {
if(isShowDialog) {
mBaseActivity.showFocusWaitDialog();
}
}
/**
*结束网络请求.
*/
public voidonFinish() {
if(isShowDialog) {
mBaseActivity.hideFocusWaitDialog();
}
}
//------------------------------抽象方法实现-------------------------------------------------
protected abstract voidonProgressNext(Tvalue);
protected abstract voidonProgressError(Throwable e);
}