Android配合RxJava嵌套接口請(qǐng)求

依賴地址

 //http日志攔截器
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.2'

//retrofit核心庫(kù)
    implementation 'com.squareup.retrofit2:retrofit:2.6.2'
//retrofit輔助,gson解析的庫(kù)
    implementation 'com.squareup.retrofit2:converter-gson:2.6.2'

// 必要依賴,和Rxjava結(jié)合必須用到
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.2'
//切換到主線程的依賴
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

ApiService的定義

public interface ApiService {

    @GET("xxxx/xxxxx/xxxxx")
    Observable<School> getSchoolInfo();

    @GET("xxxx/xxxxx/xxxxx")
    Observable<Student> getStudentInfo();
    
    
}

現(xiàn)在模擬場(chǎng)景,先調(diào)用getScholInfo()接口,獲取結(jié)果后再調(diào)用getStudentInfo()接口

        final ApiService apiService = retrofit.create(ApiService.class);
       
        apiService.getSchoolInfo().flatMap(new Function<School, ObservableSource<Student>>() {
            @Override
            public ObservableSource<Student> apply(School school) throws Exception {
                //這里可以處理返回的結(jié)果

                return apiService.getStudentInfo();//調(diào)用第二個(gè)接口
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Student>() {
                    @Override
                    public void accept(Student student) throws Exception {
                      //這里可以處理第二個(gè)接口的返回結(jié)果
                    }
                });
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容