[英]How to handle No Content response using RxJava2 et Retrofit2
I'm using RxJava 2
& Retrofit 2
( https://github.com/JakeWharton/retrofit2-rxjava2-adapter ) and I was wondering how to handle no response (204) type. 我正在使用RxJava 2
和Retrofit 2
( https://github.com/JakeWharton/retrofit2-rxjava2-adapter ),我想知道如何处理无响应(204)类型。 In rxjava1 i was using Observable<Void>
but it's not allowed by rxjava2 anymore ( https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0 -> Nulls) 在rxjava1中我使用的是Observable<Void>
但是rxjava2不再允许它( https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0 - > Nulls)
Right now, i've hacked around to bypass Json parsing on a custom type (I called it NoContent) but I was wondering if there is a better way. 现在,我已经破解了绕过自定义类型的Json解析(我称之为NoContent),但我想知道是否有更好的方法。
EDIT: 编辑:
public class NoContent {
public static class GsonTypeAdapter extends TypeAdapter<NoContent> {
@Override
public void write(JsonWriter out, NoContent value) throws IOException {
out.nullValue();
}
@Override
public NoContent read(JsonReader in) throws IOException {
return new NoContent();
}
}
}
You can use Completable
, Observable<ResponseBody>
or Observable<Response<T>>
in the case you are going to get 204
responses without getting converter exceptions. 您可以使用Completable
, Observable<ResponseBody>
或Observable<Response<T>>
,如果您要获得204
响应而不会获得转换器异常。
Probably the best option here is to use Completable
as return type. 这里最好的选择可能是使用Completable
作为返回类型。 All 2xx
responses from server will end up in onComplete
. 来自服务器的所有2xx
响应都将以onComplete
结尾。 Other will end up in onError
. 其他将最终在onError
。
In case of ResponseBody
all kind of valid HTTP responses won't be converted to Object and will end up in onNext
, including 4xx
and 5xx
responses. 在ResponseBody
情况下,所有类型的有效HTTP响应都不会转换为Object,最终将在onNext
,包括4xx
和5xx
响应。
In case of Response<T>
only 2xx responses will be converted, including, probably HTTP 204
response code. 在Response<T>
情况下,将仅转换2xx个响应,包括可能的HTTP 204
响应代码。 So I am not sure you should use it, although all valid HTTP responses in this case will also end up in onNext
, including 4xx
and 5xx
responses. 所以我不确定你应该使用它,虽然在这种情况下所有有效的HTTP响应也将在onNext
结束,包括4xx
和5xx
响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.