gpt4 book ai didi

java - Android Retrofit with Rx 获取其他 http 代码

转载 作者:太空宇宙 更新时间:2023-11-04 10:14:19 24 4
gpt4 key购买 nike

我想通过 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com