-
-
Save pyadav/c2599194432937a70293f749dafa74a3 to your computer and use it in GitHub Desktop.
RxJava and Retrofit sample
This file contains hidden or 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 hidden or 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(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