retrofit2 rxjava 2-出现错误时如何访问响应正文

问题描述 投票:0回答:1

我与Rxjava一起使用Retrofit来请求服务器。我的服务器返回定义的json格式,其中包括data和定义的消息。服务器返回http响应。服务器返回成功代码(200)是可以的。但是我想,如果服务器返回其他代码,我将管理该响应的主体。例如:服务器返回401,我想读取服务器显示消息的响应正文。但是,当服务器使用其他代码时,改造会调用onError方法,而我无法使用响应主体。如何解决这个问题?这是我的方法

'''

private void login(String username , String password){
    view.setLoading();
    source.loginUser(username, password)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new SingleObserver<Response<LoginResult>>() {
                @Override
                public void onSubscribe(Disposable d) {
                    disposable.add(d);
                }

                @Override
                public void onSuccess(Response<LoginResult> loginResult) {

                    if (loginResult.isSuccessful()){


                        }
                        else
         new AlertConfiguration(view.getViewActivity()).showMessage(loginResult.body().getMessage());

                    }



                @Override
                public void onError(Throwable e) {

           if there is a problem          
                }
            });

'''这是我的接口改造方法

@POST("...")
Single<Response<LoginResult>> loginUser(@Query("username") String username, @Query("password") String password);
android android-studio observable retrofit2 rx-java2
1个回答
1
投票

根据信息here,由于您已经在使用Response<LoginResult>,因此应该可以将Throwable强制转换为HttpException,然后提取响应的正文。

在科特林:

if (e is HttpException) {
    val errorBody = (e as HttpException).response().errorBody()
}

在Java中:

 if (e instanceof HttpException) {
     HttpException error = (HttpException)e;
     String errorBody = error.response().errorBody().string();
 }

使用您的Java代码的完整示例:

import retrofit2.HttpException

private void login(String username , String password){
    view.setLoading();
    source.loginUser(username, password)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new SingleObserver<Response<LoginResult>>() {
                @Override
                public void onSubscribe(Disposable d) {
                    disposable.add(d);
                }

                @Override
                public void onSuccess(Response<LoginResult> loginResult) {
                    if (loginResult.isSuccessful()){
                      // Handle success

                    } else {
                      // Handle login failure
                       new AlertConfiguration(view.getViewActivity()).showMessage(loginResult.body().getMessage());
                    }

                }

                @Override
                public void onError(Throwable e) {
                  // Get the error response:
                  if (e instanceof HttpException) {
                    HttpException error = (HttpException)e;
                    String errorBody = error.response().errorBody().string();
                    // Then parse the errorBody and extract the values you need
                  }
                }
            });
© www.soinside.com 2019 - 2024. All rights reserved.