James Lee James Lee - 28 days ago 7
Android Question

Retrofit with rxjava

I'm using Retrofit on Android and I want to use RxJava.

Using only Retrofit was fine but with RxJava is kind of difficult for me.

Could you tell me what to do?

I wrote a interface like below but I'm not sure what option I should use at ???????????

Thanks in advance.

@GET("users")
Observable<??????????????> getUserById (@Query("id") int id);

@GET("users")
Observable<??????????????> getUserByName (@Query("name") String name);

@GET("users")
Observable<??????????????> getAllUsers ();

Answer

As you have wrtten, you have an experience with Retrofit. Normally your network call would return Call<{type}>, where type is the object you return from API.

Here you just replace Call with Observable. So your functions would look probably like:

@GET("users")
Observable<User> getUserById (@Query("id") int id);

@GET("users")
Observable<User> getUserByName (@Query("name") String name);

@GET("users")
Observable<List<User>> getAllUsers ();

The tutorial on how to use RxJava with Retrofit can be found here

Comments