retrofit2 rxjava 2 - 有错误时如何访问响应主体
retrofit2 rxjava 2 - how can access to body of response when have error
我使用 Retrofit 和 Rxjava 一起向服务器请求。
我的服务器 return 定义了包含数据的 json 格式,并定义了消息。
服务器 return http 响应。如果服务器 return 成功代码(200).
没关系
但我想,如果服务器 return 其他代码,我管理该响应的主体。
例如:
服务器 return 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);
根据信息 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
}
}
});
我找到了解决方案
在改造 onSuccess 方法中,当响应代码为 200 时,您应该从 body 对象获得响应。
但是当不是 200 时,您应该从 errorBody 得到响应;
new Gson().fromJson(serverResultResponse.errorBody().string(), ServerResult.class);
我使用 Retrofit 和 Rxjava 一起向服务器请求。
我的服务器 return 定义了包含数据的 json 格式,并定义了消息。
服务器 return http 响应。如果服务器 return 成功代码(200).
没关系
但我想,如果服务器 return 其他代码,我管理该响应的主体。
例如:
服务器 return 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);
根据信息 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
}
}
});
我找到了解决方案
在改造 onSuccess 方法中,当响应代码为 200 时,您应该从 body 对象获得响应。
但是当不是 200 时,您应该从 errorBody 得到响应;
new Gson().fromJson(serverResultResponse.errorBody().string(), ServerResult.class);