问题描述
我使用 Rxjava 和 Retrofit 来处理来自 HTTP 服务器的响应.
I use Rxjava and Retrofit to handle the response from an HTTP server.
大多数时候,数据响应采用这种格式:
Most of the time, data response is in this format:
{
code: 'OK',
data: {.....}
}
有时,data
属性不存在:
{
code: 'OK'
}
我创建了一个 BaseResponse
类.
public class BaseResponse<T> implements Serializable {
private String code;
private String msg;
private T data;
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
public T getData() {
return data;
}
}
并制作一个Map
函数来解析它:
And make a Map
function to parse it:
public static <T> Function<BaseResponse<T>, T> applyUnwrapResponse () {
return new Function<BaseResponse<T>, T>() {
@Override
public T apply(@NonNull BaseResponse<T> baseResponse) throws Exception {
if (baseResponse.getCode().equals(Constants.SUCCESS_CODE)) {
// http returned from server
T data = baseResponse.getData();
if (data == null) {
return null; // throw exception: The mapper function returned a null value
return return new T(); // syntax error
// how ?
}
return data;
} else if (baseResponse.getCode().equals(Constants.USER_NOT_LOGGED_IN)) {
// auth token error
throw new UserNotLoginException(baseResponse.getMsg());
}
// unknown error
throw new UnknownServerException(baseResponse.getMsg());
}
};
}
当 data
属性不存在时,我尝试返回 null
,但这在 Rxjava2.
在自定义Observer中,我在onNext()中处理了所有success
场景,所以我需要在data == null
时发出错误.
但是我不能返回null,也不能通过return new T()
返回一个T
,那我该怎么办?谁能帮帮我?
When data
property doesn't exist, I try to return null
, but this is not allowed in Rxjava2.
In the customize Observer, I handle all success
scenarios in onNext(), so I need to emit an error when data == null
.
but I can not return null, and also cannot return a T
by return new T()
, so how should I do? Anyone can help me out?
推荐答案
几个建议的选项:
- 由于 Gson 解析器会在没有数据的字段中放置 null,因此您可以返回整个
BaseResponse
对象并在订阅者/观察者处检查数据属性对象的无效性. 你可以简单地用一些
Optional
类似的接口包装你的 T 值,它可以有一个值,也可以什么都没有(Java8Optional
可能不适用于android,但你可以自己想出简单的结构,比如Optional
),然后在你的onNext()
上你可以检查你是否有一个值.
- As Gson parser will put null in fields with no data, so you can return the entire
BaseResponse
object and check the nullity of data property object at the Subscriber/Observer. You can simply wrap your T value with some
Optional
alike interface, which can have a value, or can have nothing (Java8Optional
probably not available with android, but you can come up with simple structure likeOptional
yourself), then on youronNext()
you can check whether you have a value or not.
RxJava2 具有新的 Observable
类型,称为 也许,要么发出单个项目 - onSuccess
通知,要么什么都不发出 - onComplete()
通知,(还有错误):
RxJava2 has new Observable
type called Maybe, which either emit a single item - onSuccess
notification, or nothing - onComplete()
notification, (and also errors):
onSubscribe (onSuccess | onCompleted | onError)?
您可以将 flatMapMaybe
与 Observable
或 Single
(来自 Retrofit 服务接口)一起使用,将一些响应值映射到 Maybe
,在 flatMapMaybe
中,您可以使用 Maybe.fromCallable
返回 null,就像您在此处所做的那样,Maybe.fromCallable
将处理由于未发出任何项目而返回 null,您将获得 onComplete()
,因此您可以区分结果情况 (onNext
) 和无项目情况 (onComplete
).
You can use flatMapMaybe
with both Observable
or Single
(from the Retrofit service interface), to map some response value to a Maybe
, in the flatMapMaybe
you can use Maybe.fromCallable
to return null like you did here, Maybe.fromCallable
will treat returned null as no items emitted, and you'll get onComplete()
, so you can distinguish between result case (onNext
) to the no items case (onComplete
).
这篇关于RXjava 和 Retrofit 处理空数据响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!