When using rxjava 1.x i used to return Observable<Void>
to handle empty response from retrofit:
@POST( "login" ) Observable<Void> getToken( @Header( "Authorization" ) String authorization, @Header( "username" ) String username, @Header( "password" ) String password );
But since rxjava 2.x won't emit anything with Void
is there any good practice to handle those empty responses?
You can just return a ResponseBody , which will bypass parsing the response. Even better: Use Void which not only has better semantics but is (slightly) more efficient in the empty case and vastly more efficient in a non-empty case (when you just don't care about body).
Rx gives you a very granular control over which threads will be used to perform work in various points within a stream. To point the contrast here already, basic call approach used in Retrofit is only scheduling work on its worker threads and forwarding the result back into the calling thread.
RxJava, once the hottest framework in Android development, is dying. It's dying quietly, without drawing much attention to itself. RxJava's former fans and advocates moved on to new shiny things, so there is no one left to say a proper eulogy over this, once very popular, framework.
Completable was designed for such cases. It available since RxJava 1.1.1. From the official docs:
Represents a deferred computation without any value but only indication for completion or exception. The class follows a similar event pattern as Reactive-Streams: onSubscribe (onError|onComplete)?
So just change your method's return type:
@POST("login") Completable getToken(@Header("Authorization") String authorization, @Header("username") String username, @Header("password") String password);
And rewrite your subscriber, e.g.:
apiManager.getToken(auth, name, pass) ... .subscribe(() -> { //success }, exception -> { //error });
Another solution is:
@POST("login") Observable<Response<Void>> getToken(@Header("Authorization") String authorization, @Header("username") String username, @Header("password") String password);
Update: But I would rather use Completable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With