RX Java Retrofit 2错误处理 [英] rx java retrofit 2 error handling
问题描述
我正在与rx java一起使用改造2
I'm using retrofit 2 along with rx java
情况:该应用程序发送了一些请求,然后我得到json格式的响应,该响应会自动转换为User dto,然后以rx java的onNext方法接收用户列表.如果我从服务器收到以下消息,该怎么办:{错误":无法获取用户列表"}
Situation: the app sends some request, then i get the response in json-format that is automatically converted to User dto, then in onNext method of rx java i receive the list of Users. What if i get some message from server like this: {"error":"can't get the list of users"}
如何通过翻新2和rx处理这种情况?
how to handle this situation with retrofit 2 and rx?
Subscription subscriptionBranches = model.getRepoBranches(owner, name)
.map(branchesMapper)
.subscribe(new Observer<List<Branch>>() {
@Override
public void onCompleted() {
;
}
@Override
public void onError(Throwable e) {
if (e instanceof retrofit.HttpException) {
HttpException exception = (HttpException) e;
}
showError(e);
}
@Override
public void onNext(List<Branch> list) {
branchList = list;
view.showBranches(list);
}
});
addSubscription(subscriptionBranches);
.....
@Override
public Observable<List<RepositoryDTO>> getRepoList(String name) {
return apiInterface
.getRepositories(name)
.compose(applySchedulers());
}
推荐答案
取决于服务器响应,您可能会也可能不会进入 onError
函数.如果服务器返回的非2XX http状态代码,您将进入 onError
方法.另一方面,如果您收到2XX http状态代码,则输入 onNext
.
Depending on the server response you might or might not get into your onError
function. If the server returns a non-2XX http status code you'll get into the onError
method. If on the other hand you get a 2XX http status code you'll enter onNext
.
我假设您可以处理 onNext
位,并且我将在 onError
中说明如何进行操作.重要的是要意识到执行此操作的方法很多,这只是使用 okhttp 3 和 retrofit 2 beta4 的示例.
I'm assuming you can deal with the onNext
bit and I'll explain how you can do it in the onError
. It's important to realise that there are many ways of doing this and this is just an example that uses okhttp 3 and retrofit 2 beta4.
因此Retrofit2表示,使用 rxjava
时,每个非2XX http响应都是 HttpException
.您已经在代码中找到了它:
So retrofit2 says that every non-2XX http responses are HttpException
s when using rxjava
. This you already have it there in your code:
if (e instanceof retrofit.HttpException) {
HttpException exception = (HttpException) e;
}
现在,您要做的就是获取响应的正文.您可以通过在 HttpException
中调用 Response response = exception.response()
来实现.有了响应,就可以很容易地得到错误体.您只需调用 response.errorBody()
.然后,您可以将主体转换为java对象,或者只是将其作为字符串访问.
Now what you want to do is get the body of the response. This you can achieve by calling Response response = exception.response()
in the HttpException
you have there. With the response, getting the error body is quite straight forward. You just call response.errorBody()
. You can then convert the body to a java object or just access it as a string.
由于您有一个 json
错误正文作为示例,因此可以将响应正文转换为Java对象:
Since you have a json
error body as an example, here's how you can convert the response body to a java object:
new GsonConverterFactory().responseBodyConverter(type,
new Annotation[0]).convert(response.errorBody());
其中 type
是表示错误的java对象的类.
where type
is the class of the java object that represents the error.
因此,将它们放在一起,在您的 onError
方法上,您可以编写如下内容:
So putting it all together, on your onError
method you could write something like:
if (e instanceof retrofit.HttpException) {
HttpException exception = (HttpException) e;
Response response = exception.response();
Converter<ResponseBody, MyError> converter = new GsonConverterFactory()
.responseBodyConverter(MyError.class, Annotation[0]);
MyError error = converter.convert(response.errorBody());
}
MyError
是一个模型,表示您在问题中遇到的错误json.
MyError
is a model that represents the error json you have in your question.
这篇关于RX Java Retrofit 2错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!