I am new to Rx java and I have a scenario where I want to get data from multiple APIs. These APIs are independent of each other but I want to show data from these APIs in a View. So I want to make these APIs calls in such a manner so that I can get every API data at the same time. I already using retrofit 2. I know a little bit about RX JAVA, but I only know how to make one request at a time. Please help
Retorfit Rest Client:
public class RestClient {
private static ApiService apiService = null;
public static ApiService getApiService(String url) {
apiService = null;
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okClient = new OkHttpClient.Builder().
connectTimeout(1, TimeUnit.MINUTES).
readTimeout(3, TimeUnit.MINUTES).
addInterceptor(interceptor).build();
Gson gson = new GsonBuilder().setLenient().create();
Retrofit = new Retrofit.Builder()
.baseUrl(url).client(okClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
apiService = restAdapter.create(ApiService.class);
return apiService;
}
}
Api Service Interface:
public interface ApiService {
@GET("v2/nearest_city")
Observable<AqiDto> getAQI(@Query("lat") String latitude,
@Query("lon") String longitude,
@Query("key") String key);
@GET("data/2.5/weather")
Observable<WeatherDTO> getWeather(@Query("lat") String latitude,
@Query("lon") String longitude,
@Query("appid") String id);
}
Repository Class:
public class SiteListRepository {
private static final String TAG = "SITE_LIST_REPO";
private final CompositeDisposable disposable;
private Context mContext;
private AppUtilities mAppUtilities;
public SiteListRepository(Application application) {
mContext = application.getApplicationContext();
disposable = new CompositeDisposable();
mAppUtilities = new AppUtilities(mContext);
}
public Object getData() {
disposable = Observable.merge(RestClient.getApiService(BASE_URL_AIR_INDEX).getAQI(EcoHubApplication.mAppSharedPreference.getLatitude(), EcoHubApplication.mAppSharedPreference.getLongitude(), "2664b262-6369-415c-aa5a-ef2bd9ccf1cf")
.subscribeOn(Schedulers.newThread()), RestClient.getApiService(BASE_URL_OPEN_WEATHER).getWeather(EcoHubApplication.mAppSharedPreference.getLatitude(), EcoHubApplication.mAppSharedPreference.getLongitude(), "b6907d289e10d714a6e88b30761fae22")
.subscribeOn(Schedulers.newThread())).observeOn(AndroidSchedulers.mainThread()).subscribe(obj -> {
object = obj;
});
return object;
}
}
Want to merge these 2 APIs calls together.
ViewModel:
public class SiteListViewModel extends AndroidViewModel {
private SiteListRepository siteListRepository;
public MutableLiveData<AqiDto> aqiDTOMutableLiveData = new MutableLiveData<>();
public MutableLiveData<WeatherDTO> weatherDTOMutableLiveData = new MutableLiveData<>();
private Object object;
public SiteListViewModel(@NonNull Application application) {
super(application);
siteListRepository = new SiteListRepository(application);
}
public void getData(){
object = siteListRepository.getData();
if (object instanceof AqiDto){
aqiDTOMutableLiveData.setValue((AqiDto) object);
} else if (object instanceof WeatherDTO){
weatherDTOMutableLiveData.setValue((WeatherDTO) object);
}
}
}
Single.zip(...)orSingle.zipArray(...)Single.merge(...). Also, what do you mean by 'not the most effective way'? According to your usage, zip will be able to achieve what you want.