在做乐享大学客户端的时候遇到这样的情况,就是要在用户登陆的情况下要获取的用户个人数据跟首页的数据进行合并再传递到activity,这里用RxJava2 的关键字zip 实现。
代码语言:javascript复制 private Observable<Data> getHomeData(String url){
OkHttpClient client=new OkHttpClient();
client.newBuilder().connectTimeout(10, TimeUnit.SECONDS);
retrofit=new Retrofit.Builder()
.baseUrl(url)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
apiService=retrofit.create(ApiServices.class);
return apiService.getHomeData("1");
}
private Observable<UserBean> getUniData(String url,String token,String deviceId){
OkHttpClient client=new OkHttpClient();
client.newBuilder().connectTimeout(10, TimeUnit.SECONDS);
retrofit=new Retrofit.Builder()
.baseUrl(url)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
apiService=retrofit.create(ApiServices.class);
return apiService.getUserBean(token,deviceId);
}
这里返回两个Observable对象;在downloadData进行合并数据
代码语言:javascript复制 Observable<Data> homeData=getHomeData(url);
//优化如果没有登陆,不执行。
if(token!=null) {
Observable<UserBean> uniData = getUniData(urlUv, token, deviceId);
Observable.zip(homeData, uniData, new BiFunction<Data, UserBean, HomeData>() {
@Override
public HomeData apply(@NonNull Data data, @NonNull UserBean userBean) throws Exception {
HomeData homeData = new HomeData();
homeData.setD_title(data.getQuanData().get(0).getD_title());
homeData.setPic(data.getQuanData().get(0).getPic());
homeData.setPrice(data.getQuanData().get(0).getPrice() "");
homeData.setPicData(data.getActivityPic());
homeData.setRotationPic(data.getRotationPic());
if (!userBean.isError()) {
homeData.setUserBean(userBean);
}else{
homeData.setUniError(userBean.getError_msg());
}
return homeData;
}
})
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<HomeData>() {
Disposable d;
@Override
public void onSubscribe(@NonNull Disposable d) {
this.d = d;
}
@Override
public void onNext(@NonNull HomeData homeData) {
iDownloadHomeDataPresenter.downloadSuccess(homeData);
if(homeData.getUniError()!=null){
iDownloadHomeDataPresenter.downloadUniFail(homeData.getUniError());
}
onComplete();
}
@Override
public void onError(@NonNull Throwable e) {
iDownloadHomeDataPresenter.downloadFail();
onComplete();
}
@Override
public void onComplete() {
d.dispose();
}
});
}else{
homeData.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Data>() {
@Override
public void accept(Data data) throws Exception {
HomeData homeData = new HomeData();
homeData.setD_title(data.getQuanData().get(0).getD_title());
homeData.setPic(data.getQuanData().get(0).getPic());
homeData.setPrice(data.getQuanData().get(0).getPrice() "");
homeData.setPicData(data.getActivityPic());
homeData.setRotationPic(data.getRotationPic());
iDownloadHomeDataPresenter.downloadSuccess(homeData);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
//这里接收onError
iDownloadHomeDataPresenter.downloadFail();
}
}, new Action() {
@Override
public void run() throws Exception {
//这里接收onComplete。
}
});
}