文章 16
浏览 11957
Retrofit中使用Rxjava2的zip操作符进行多线程请求

Retrofit中使用Rxjava2的zip操作符进行多线程请求

基本思路

使用subscribeOn(Schedulers.io());在线程中获取多个Observable,再使用.zip操作同步到(UI)主线程中,其中关于BiFunction中,返回的数据使用新建一个AllBean(找解决办法ing)

代码区

WApi
1public interface WApi {
2    @Headers("token:a337xZZFXIn7SqD1")
3    @GET("dog")
4    Observable<DogBean> getDogJson();
5    @Headers("token:a337xZZFXIn7SqD1")
6    @GET("qinghua")
7    Observable<QingHuaBean> getQinghuaJson();
8}
RetrofitManager
 1public class RetrofitManager {
 2
 3    private static RetrofitManager sInstance = new RetrofitManager();
 4    private WApi mRetrofit;
 5
 6    public static RetrofitManager getInstance() {
 7        return sInstance;
 8    }
 9
10    public static WApi getRetrofit() {
11        return sInstance.mRetrofit;
12    }
13    private RetrofitManager() {
14
15        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
16            @Override
17            public void log(String message) {
18                Log.d("Http", message);
19            }
20        });
21        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
22        OkHttpClient okHttpClient = new OkHttpClient.Builder()
23                .addInterceptor(logging)
24                .build();
25
26        mRetrofit = new Retrofit.Builder()
27                .client(okHttpClient)
28                .addConverterFactory(GsonConverterFactory.create())
29                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
30                .baseUrl("https://v2.alapi.cn/api/")
31                .build()
32                .create(WApi.class);
33    }
34}
MainActivity
 1public class MainActivity extends AppCompatActivity implements View.OnClickListener{
 2    private TextView mMainTV1;
 3    private TextView mMainTV2;
 4    private Button mMainBT;
 5    @Override
 6    protected void onCreate(Bundle savedInstanceState) {
 7        super.onCreate(savedInstanceState);
 8        setContentView(R.layout.activity_main);
 9        initView();
10        mMainBT.setOnClickListener(this);
11    }
12
13    private void initView() {
14        mMainTV1 = findViewById(R.id.main_tv1);
15        mMainTV2 = findViewById(R.id.main_tv2);
16        mMainBT = findViewById(R.id.main_bt);
17    }
18    @Override
19    public void onClick(View v) {
20        switch (v.getId()) {
21            case R.id.main_bt:
22                WApi wApi = RetrofitManager.getRetrofit();
23                Observable<DogBean> dogOB = wApi.getDogJson().subscribeOn(Schedulers.io());
24                Observable<QingHuaBean> qinghuaOB = wApi.getQinghuaJson().subscribeOn(Schedulers.io());
25                Observable.zip(dogOB, qinghuaOB, new BiFunction<DogBean, QingHuaBean, AllBean>() {
26                    @NonNull
27                    @Override
28                    public AllBean apply(@NonNull DogBean dogBean, @NonNull QingHuaBean qingHuaBean) throws Exception {
29                        AllBean allBean = new AllBean();
30                        allBean.setDogBean(dogBean);
31                        allBean.setQingHuaBean(qingHuaBean);
32                        return allBean;
33                    }
34                }).observeOn(AndroidSchedulers.mainThread())
35                        .subscribe(new Observer<AllBean>() {
36                            @Override
37                            public void onSubscribe(@NonNull Disposable d) {
38
39                            }
40
41                            @Override
42                            public void onNext(@NonNull AllBean allBean) {
43                                    mMainTV1.setText(allBean.getDogBean().getData().getContent());
44                                    mMainTV2.setText(allBean.getQingHuaBean().getData().getContent());
45                            }
46
47                            @Override
48                            public void onError(@NonNull Throwable e) {
49
50                            }
51
52                            @Override
53                            public void onComplete() {
54
55                            }
56                        });
57        }
58    }
59
60}

标题:Retrofit中使用Rxjava2的zip操作符进行多线程请求
作者:kuohai
地址:https://udday.cn/articles/2021/03/11/1615446087216.html

充实的一天

取消