在使用retrofit+rxjava 的时候好像不知道哪里找请求失败的错误码 其实在observer的回调的方法中 onError方法中去判断Throwable 的类型,如果是httpException的话 就强转一下就找到了
messageCodeBeanObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<LoginCodeBean>() {
@Override
public void onCompleted() {
messageCodeBeanHttpsInterface.onComplete();
}
@Override
public void onError(Throwable e) {
if (e instanceof HttpException){
HttpException httpException = (HttpException) e;
try {
int code = httpException.code();
byte[] s = httpException.response().errorBody().bytes();
String errordata = new String(s);
LoginCodeBean loginCodeBean = new Gson().fromJson(errordata, LoginCodeBean.class);
messageCodeBeanHttpsInterface.onFailure(loginCodeBean.getMsg());
} catch (IOException ex) {
ex.printStackTrace();
}
}else {
messageCodeBeanHttpsInterface.onError("网络请求失败");
}
}
@Override
public void onNext(LoginCodeBean messageCodeBean) {
//判断是否请求成功
if (messageCodeBean.getCode()==200){
messageCodeBeanHttpsInterface.onSuccess(messageCodeBean);
}else {
messageCodeBeanHttpsInterface.onFailure(messageCodeBean.getMsg());
}
}```