How does the chain ring?

How can I make one repeat call 2 after another?

I am reading about RxJava and I am already making my calls using RxJava, but I have not found a good example of using flatMaps.

Can someone explain how to do this with me?

I am trying to make these two calls, and after they are both made, I want to start a new job.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Retrofit retrofit = new Retrofit.Builder()
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("http://api.openweathermap.org/data/2.5/")
            .build();

    WeatherService weatherService = retrofit.create(WeatherService.class);
    final Observable<Weather> london = weatherService.getCurrent();

    london.subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<Weather>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(Weather weather) {

                    Log.i("WEATHER","Weather Name: " + weather.getName());


                }
            });

    final Observable<Wind> windObservable = weatherService.getWind();
    windObservable.subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<Wind>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(Wind wind) {

                  Log.i("WEATHER","Wind: " + wind.getSpeed().toString());  

                }
            });

}


}
+4
source share
3 answers

Perhaps this link: https://github.com/ReactiveX/RxJava/wiki/Combining-observables will help. Place an order for a zipper. In the end, the SwitchMap method may be useful in your case.

: , http://joluet.imtqy.com/blog/2014/07/07/rxjava-retrofit/ .

# 2:

login().switchMap(new Func1<FirstResponse, Observable<SecondResponse>>() {
            @Override
            public Observable<SecondResponse> call(FirstResponse t) {
                if (ApiUtils.isLoginValid(t)) {
                        return profile(t.getToken());
                    }
                    else{
                        return Observable.error(new CustomException());
                    }
                }

            }
        }).subscribe(subscriber());

: - Observable<SecondResponse>, - Subscriber<? super SecondResponse>

+2

flatMap, , concatWith:

static Observable<Integer> intObservable() {
    return Observable.just(1).delay(1, TimeUnit.SECONDS);
}

static Observable<String> stringObservable() {
    return Observable.interval(1, TimeUnit.SECONDS).take(2).map(v -> v.toString());
}

public static void main(String[] args) {
    intObservable()
        .doOnNext(System.out::println)
        .ignoreElements()
        .cast(Object.class)
    .concatWith(stringObservable()
            .doOnNext(System.out::println)
            .ignoreElements())
    .toBlocking()
    .subscribe();
}

, , .

0

. - zip:

london.zipWith(weatherService, new Func2<Weather, Wind, Something>() {
    @Override
    public Something call(final Weather weather, final Wind wind) {
        ...
        return something;
    }
}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Something>() {
    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(final Throwable e) {

    }

    @Override
    public void onNext(final Something something) {

    }
});
0

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


All Articles