| Start |
|---|
| Retrofit2 with RxJava First, add relevant dependencies into the build.gradle file. Then create the model you would like to receive: Create an interface containing methods used to exchange data with remote server: Then create a Then, anywhere from the code, call the method: |
| Retrofit with RxJava to fetch data asyncronously From the GitHub repo (opens new window) of RxJava, RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences. It extends the observer pattern to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronisation, thread-safety and concurrent data structures. Retrofit (opens new window) is a type-safe HTTP client for Android and Java, using this, developers can make all network stuff much more easier. As an example, we are going to download some JSON and show it in RecyclerView as a list. Getting started: Add RxJava, RxAndroid and Retrofit dependencies in your app level build.gradle file:
Define ApiClient and ApiInterface to exchange data from server public interface ApiInterface { Note the getRepos() is returning an Observable and not just a list of issues. Define the models An example for this is shown. You can use free services like JsonSchema2Pojo (opens new window) or this. Create Retrofit instance
Then, Use this instance to fetch data from server Now, you have successfully fetched data from a server using Retrofit and RxJava. |
| Nested requests example: multiple requests, combine results Suppose we have an API which allows us to get object metadata in single request ( } |
Read Article
https://develop.horje.com/learn/1434/reference