ProgressCallback

public abstract class ProgressCallback implements Callback {

privateBaseActivitymBaseActivity;

private booleanisShowDialog;

privateObjecttag;

publicProgressCallback(BaseActivity baseActivity) {

this(baseActivity, true);

}

publicProgressCallback(BaseActivity baseActivity, booleanisShowDialog) {

if(baseActivity ==null)throw newNullPointerException();

this.mBaseActivity= baseActivity;

this.isShowDialog= isShowDialog;

onStart();

}

@Override

public voidonResponse(Call call,Response response) {

LogUtil.net("onResponse");

call.cancel();

if(response.body() ==null) {

onFailure(call, newNullPointerException("response.body() is null"));

}else{

onProgressSuccess(call,response.body());

onFinish();

}

}

@Override

public voidonFailure(Call call,Throwable e) {

LogUtil.err("onFailure"+ e.getMessage());

e.printStackTrace();

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(einstanceofAppException) {

ToastUtil.showLong(e.getMessage());

//TODO: 2017/6/12统一处理异常

AppException exception = (AppException) e;

if(exception.errorCode== HttpCode.TOKEN_INVALID) {

//AppStatusTracker.getInstance().setAppStatus(ConstantValue.STATUS_KICK_OUT);

mBaseActivity.kickOut();

}

}else{

ToastUtil.showLong(e.toString());

}

onProgressFailure(call,e);

onFinish();

}

public voidonStart() {

if(isShowDialog)mBaseActivity.showFocusWaitDialog();

}

public voidonFinish() {

if(isShowDialog)mBaseActivity.hideFocusWaitDialog();

}

//------------------------------抽象方法实现-------------------------------------------------

protected abstract voidonProgressSuccess(Call call,Tresult);

protected abstract voidonProgressFailure(Call call,Throwable t);

}

你可能感兴趣的:(ProgressCallback)