Search in sources :

Example 1 with TestApis

use of com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis in project AndroidQuick by ddnosh.

the class Network3Fragment method clickSyncFlatMap.

public void clickSyncFlatMap() {
    TestApis testApis = RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(TestApis.class);
    // 先访问唐诗宋词接口
    testApis.getTangShiSongCi().subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).flatMap(new Function<TSSCRes<TSSCResult>, ObservableSource<XHYRes<XHYResult>>>() {

        @Override
        public ObservableSource<XHYRes<XHYResult>> apply(TSSCRes<TSSCResult> listTSSCRes) throws Exception {
            if (listTSSCRes != null && listTSSCRes.getResult() != null) {
                // 再访问歇后语接口
                return testApis.getXHY();
            } else {
                return null;
            }
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<XHYRes<XHYResult>>() {

        @Override
        public void onError(ApiException exception) {
            LogUtil.e(TAG, "error:" + exception.getMessage());
        }

        @Override
        public void onSuccess(XHYRes<XHYResult> testBeans) {
            LogUtil.i(TAG, testBeans.toString());
        }
    });
}
Also used : BiFunction(io.reactivex.functions.BiFunction) Function(io.reactivex.functions.Function) TestApis(com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Example 2 with TestApis

use of com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis in project AndroidQuick by ddnosh.

the class Network3Fragment method clickAsyncMerge.

private void clickAsyncMerge() {
    TestApis testApis = RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(TestApis.class);
    Observable<TSSCRes<TSSCResult>> o1 = testApis.getTangShiSongCi();
    Observable<XHYRes<XHYResult>> o2 = testApis.getXHY();
    Observable.merge(o1, o2).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(new BaseObserver<Object>() {

        @Override
        public void onError(ApiException exception) {
            LogUtil.e(TAG, "error:" + exception.getMessage());
        }

        @Override
        public void onSuccess(Object object) {
            if (object instanceof TSSCRes) {
                LogUtil.i(TAG, object.toString());
            } else if (object instanceof XHYRes) {
                LogUtil.i(TAG, object.toString());
            }
        }
    });
}
Also used : TestApis(com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Example 3 with TestApis

use of com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis in project AndroidQuick by ddnosh.

the class Network3Fragment method clickAsyncZip.

private void clickAsyncZip() {
    TestApis testApis = RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(TestApis.class);
    Observable<TSSCRes<TSSCResult>> o1 = testApis.getTangShiSongCi();
    Observable<XHYRes<XHYResult>> o2 = testApis.getXHY();
    Observable.zip(o1, o2, new BiFunction<TSSCRes<TSSCResult>, XHYRes<XHYResult>, Integer>() {

        @Override
        public Integer apply(TSSCRes<TSSCResult> tsscRes, XHYRes<XHYResult> xhyRes) throws Exception {
            return tsscRes.getResult().size() + xhyRes.getResult().size();
        }
    }).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(new BaseObserver<Integer>() {

        @Override
        public void onError(ApiException exception) {
            LogUtil.e(TAG, "error:" + exception.getMessage());
        }

        @Override
        public void onSuccess(Integer count) {
            LogUtil.e(TAG, "count = " + count);
        }
    });
}
Also used : BiFunction(io.reactivex.functions.BiFunction) TestApis(com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Example 4 with TestApis

use of com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis in project AndroidQuick by ddnosh.

the class Network3Fragment method clickSyncConcat.

private void clickSyncConcat() {
    TestApis testApis = RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(TestApis.class);
    Observable<TSSCRes<TSSCResult>> o1 = testApis.getTangShiSongCi();
    Observable<XHYRes<XHYResult>> o2 = testApis.getXHY();
    Observable.concat(o1, // 依次处理, 先处理o1, 再处理o2
    o2).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(new BaseObserver<Object>() {

        @Override
        public void onError(ApiException exception) {
            LogUtil.e(TAG, "error:" + exception.getMessage());
        }

        @Override
        public void onSuccess(Object object) {
            if (object instanceof TSSCRes) {
                LogUtil.i(TAG, object.toString());
            } else if (object instanceof XHYRes) {
                LogUtil.i(TAG, object.toString());
            }
        }
    });
}
Also used : TestApis(com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Aggregations

TestApis (com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis)4 ApiException (com.androidwind.androidquick.module.exception.ApiException)4 BiFunction (io.reactivex.functions.BiFunction)2 Function (io.reactivex.functions.Function)1