你是否还这样写网络请求(那你就走远了):
view.showProgress();
subscription = Order().search(new ParamsBuilder().put("xxxxx","xxxxx").build())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Response>() {
@Override
public void call(Response response) {
view.hideProgress();
if (response.isSuccess())
view.xxxxxxxxxx(response.getList());
else view.showError(response.getMsg());
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable e) {
e.printStackTrace();
view.hideProgress();
view.showErrorMessage(e.getMessage());
}
});
xxxxx.addSubscription(subscription);
其中view是MVP模式 中activity/fragment的接口
我封装的代码:
/**
* the base response callback entity
* Created by deadline on 16/8/8.
*
* onFinish()方法一定会被调用
*/
public abstract class OnRequestCallback<T extends Response> extends Subscriber<T> {
public abstract void onFailed(int code, String message);
public abstract void onException(String message);
public abstract void onResponse(T response);
public abstract void onFinish();
@Override
public void onStart() {
if (!NetUtil.isConnected()){
onFailed( -1, App.getContext().getString(R.string.network_unavailable));
onFinish();
unsubscribe();
return;
}
}
@Override
public final void onCompleted() {
onFinish();
}
@Override
public final void onError(Throwable e)
{
onException(e.getMessage());
onFinish();
}
@Override
public final void onNext(T response)
{
if(response.isSuccess())
{
onResponse(response);
}else {
onFailed(response.getCode(), response.getMessage());
}
}
}
/**
* 不需要展示进度框的网络请求, 但是出现错误会提示
* 如果你想使用其他方式展示错误信息, 回调里重写方法即可
* 该类是abstract的.
* T.showLong(); 是toast的工具类
* Created by deadline on 16/8/8.
*/
public abstract class OnSimpleRequestCallback<T extends Response> extends OnRequestCallback<T> {
@Override
public void onStart() {
super.onStart();
}
@Override
public void onFinish() {
};
@Override
public void onFailed(int code, String message){
T.showLong(message);
};
@Override
public void onException(String message){
T.showLong(message);
};
}
/**
* 需要展示进度框的网络访问
*
* 如果在onResponse/onFailed/onException 任何一个方法里调用了BaseView.finish
* 可能activity无法及时释放,因为OnProgressRequestCallback还持有view,
* 因此在使用的时候注意,如果有以上情况,请在finish activty
* 之前调用 OnProgressRequestCallback.onFinish ,
* 不调用也没关系,因为是WeakReference<BaseView>.
*
* Created by deadline on 16/8/8.
*/
public abstract class OnProgressRequestCallback<T extends Response>
extends OnSimpleRequestCallback<T>{
private WeakReference<BaseView> reference;
public OnProgressRequestCallback(BaseView baseView){
reference = new WeakReference<BaseView>(baseView);
}
/**
* 注意这里代码顺序一定不能错
*/
@Override
public void onStart() {
reference.get().showProgress();
super.onStart();
}
/**
* onFinish()一定会被调用
*/
@Override
public void onFinish() {
reference.get().hideProgress();
}
}
如何用:
没错就这样用:
不用关心错误/异常提醒,
不用关心网络未连接提醒,
不用关心progressDialog显示隐藏.
不用谢我,能用到,就拿走。别忘了点个赞!
xxxxx
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new OnProgressRequestCallback<Response>(mView) {
@Override
public void onResponse(Response response) {
}
});
Response类:
public class Response {
private String message;
private int code;
public boolean isSuccess(){
return code == 0;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void setCode(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}