Combining two web service calls using RxJava and retrofitting

I am using RxJava and Retrofit. My basic requirement is that I want to associate two api calls that will be called one by one. The response received from the first api is used as input during the call of the second api. After reading some things on the Internet, I used layouts to achieve this. During this operation, I show the bootloader. Sometimes it works smoothly, but in some cases this bootloader freezes. DDMS shows a log of "300 frames missed, the application can work too much on its main thread." I suspect one of my network calls is working in the main thread. I cannot figure out how to connect these two calls so that they can be called smoothly in the background without interfering with my main thread. Any help is appreciated. thanks in advancewhat i tried so far

private CompositeSubscription mSubscriptions = new CompositeSubscription();

Subscription subscription = Observable.just(getAddress())
          .subscribeOn(Schedulers.newThread())
          .flatMap(address -> mPlatformApi.secondWebService(address.getLatitude(),address.getLongitude())
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe(modelTwo ->
          {
            //updating My ui
          }, throwable -> {

            //Error Handling
          });

mSubscriptions.add(subscription);


private android.location.Address getAddress(){
    String addressString = "";//some Address String
    Geocoder coder = new Geocoder(getActivity());
    android.location.Address address=null;
    try {
      ArrayList<android.location.Address> addressList = (ArrayList<android.location.Address>) coder.getFromLocationName(addressString, 1);
      if(addressList !=null && addressList.size()>0) {
        address = addressList.get(0);
      } else {

      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return address;
  }

//My Retrofit call
Observable<modelTwo> secondWebService(@Path("lat") double lat,@Path("lon") double lon);
+4
1

:

final android.location.Address address = getAddress();
Subscription subscription = Observable.just(address) ...

, , getAddress() , RxJava . , just, subscribeOn ( onNext(address) Subscriber) . , .. getAddress, , .

getAddress - defer:

Subscription subscription = Observable.defer(new
          Func0<Observable<android.location.Address>>() {

              @Override
              public Observable<android.location.Address> call() {
                  return Observable.just(getAddress());
              }
          })
          .subscribeOn(Schedulers.newThread())
          .flatMap(address -> mPlatformApi.secondWebService(address.getLatitude(),address.getLongitude()    )
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe(modelTwo ->
          {
            //updating My ui
          }, throwable -> {
            //Error Handling
          });

, Func0 newThread() - just, getAddress.

+6

Source: https://habr.com/ru/post/1616059/


All Articles