Skip to content

Instantly share code, notes, and snippets.

@cesarferreira
Last active February 1, 2022 16:32
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save cesarferreira/da1e8fc5742ab1e581b7 to your computer and use it in GitHub Desktop.
Save cesarferreira/da1e8fc5742ab1e581b7 to your computer and use it in GitHub Desktop.
RxJava and Retrofit sample
public interface API {
@GET("/user/{username}/dogs")
Observable<Dog> getAllDogsOf(@Path("username") String username);
@GET("/dog/{id}")
Observable<Dog> getDogInfoById(@Path("id") int dogId);
}
new RestService().getAllDogsOf("cesarferreira")
.doOnSubscribe(() -> { /* starting request */
// TODO show Loading Spinner
})
.doOnCompleted(() -> { /* finished request */
// TODO hide Loading Spinner
})
// request webservice for each dog id with all info
.flatMap(dogId -> new RestService().getDogInfoById(dogId))
.doOnError(throwable -> {
/* log the error */
})
.onErrorResumeNext(Observable.<~>empty())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(dog -> {
// TODO do what you want with the dog object
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment