Skip to content

Instantly share code, notes, and snippets.

@ruben-h
Forked from cesarferreira/api.java
Created September 28, 2015 12:19
Show Gist options
  • Save ruben-h/f84ef1825ae044c79e4a to your computer and use it in GitHub Desktop.
Save ruben-h/f84ef1825ae044c79e4a 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(dogs -> {
// TODO do what you want with the list of Dogs
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment