我想通过 retrofit rx 获取相同的其他 http 代码数据 400 、 401 和其他数据 这是我的代码
@GET("sample")
Observable<String> getSample();
&
public Observable<String> getSample() {
return Domain.getApiClient(Tags.WRITHE_URL).getSample()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.unsubscribeOn(Schedulers.io());
}
&
new AppStore().getSample().subscribe(new DisposableObserver<String>() {
@Override
public void onNext(String value) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
当http代码200时我可以在下一个中使用数据 和其他http代码调用错误 在 onError 中我没有服务器的访问数据 当http代码不是200时我应该怎么做才能获取数据?
最佳答案
非 200 响应信息包含在传递给 onError
的异常中:
public void onError(Throwable e){
HttpException error = (HttpException)e; //you can use the instanceof check
int errorCode = error.response().code();
String errorBody = error.response().errorBody().string();
}
关于java - Android Retrofit with Rx 获取其他 http 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51907632/