-
-
Save dupengtao/9eb57bae7669bf827db0 to your computer and use it in GitHub Desktop.
RxJava and Retrofit sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface API { | |
@GET("/user/{username}/dogs") | |
Observable<Dog> getAllDogsOf(@Path("username") String username); | |
@GET("/dog/{id}") | |
Observable<Dog> getDogInfoById(@Path("id") int dogId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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