Skip to content

Instantly share code, notes, and snippets.

@dupengtao
Forked from cesarferreira/api.java
Created March 16, 2016 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dupengtao/9eb57bae7669bf827db0 to your computer and use it in GitHub Desktop.
Save dupengtao/9eb57bae7669bf827db0 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