连锁改造如何调用?

编程入门 行业动态 更新时间:2024-10-23 06:23:11
本文介绍了连锁改造如何调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何进行一次又一次的改造?

How can I make one retrofit 2 call after another?

我正在阅读有关 RxJava 的文章,并且我已经在使用 RxJava 进行调用,但是我还没有找到如何使用 flatMaps 的好例子.

I'm reading about RxJava and I'm already doing my calls using RxJava, but I havn't found a good exemple of how to use flatMaps.

有人可以向我解释如何做吗?

Can someone explain how to do it to me?

我正在尝试拨打这两个电话,在这两个电话都完成后,我想开始一项新活动.

I'm trying to make these two calls, and after they're both done, I want to start a new activity.

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("api.openweathermap/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()); } }); } }

推荐答案

也许这个链接:github/ReactiveX/RxJava/wiki/Combining-observables 会有所帮助.结帐 zip.最终 switchMap 方法可能对您有用.

Maybe this link: github/ReactiveX/RxJava/wiki/Combining-observables will help. Checkout for zip. Eventually switchMap method may be useful in Your case.

也许这个例子 joluet.github.io/blog/2014/07/07/rxjava-retrofit/ 会帮助你更多.

Maybe this example joluet.github.io/blog/2014/07/07/rxjava-retrofit/ will help You even more.

编辑 #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());

注意:profile方法返回类型是Observable,订阅者方法类型是Subscriber

Note: profile method return type is is Observable<SecondResponse> and subscriber method type is Subscriber<? super SecondResponse>

更多推荐

连锁改造如何调用?

本文发布于:2023-10-31 07:29:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545435.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!