Search in sources :

Example 6 with ApiException

use of com.androidwind.androidquick.module.exception.ApiException in project AndroidQuick by ddnosh.

the class Network1Presenter method initData.

@Override
public void initData(String type) {
    if ("get".equals(type)) {
        // 获取接口实例
        Gank2Apis gank2Apis = RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(Gank2Apis.class);
        // 调用方法得到一个Call
        Call<GankRes<List<String>>> call = gank2Apis.getHistoryDate();
        // 进行网络请求
        call.enqueue(new Callback<GankRes<List<String>>>() {

            @Override
            public void onResponse(Call<GankRes<List<String>>> call, Response<GankRes<List<String>>> response) {
                getView().updateView(response.body().getResults().toString());
            }

            @Override
            public void onFailure(Call<GankRes<List<String>>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    } else {
        RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(GankApis.class).getHistoryDate().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<GankRes<List<String>>>() {

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

            @Override
            public void onSuccess(GankRes<List<String>> listGankRes) {
                LogUtil.i(TAG, listGankRes.getResults().toString());
                getView().updateView(listGankRes.getResults().toString());
            }
        });
    }
}
Also used : Gank2Apis(com.androidwind.androidquick.demo.features.module.network.retrofit.Gank2Apis) GankRes(com.androidwind.androidquick.demo.features.module.network.retrofit.GankRes) List(java.util.List) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Example 7 with ApiException

use of com.androidwind.androidquick.module.exception.ApiException 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)

Example 8 with ApiException

use of com.androidwind.androidquick.module.exception.ApiException in project AndroidQuick by ddnosh.

the class RxJavaFragment method testError.

// --------Error
private void testError() {
    // onErrorReturn: 出现错误的时候,用一个默认的数据项将错误替代
    Observable.fromCallable(new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            // 返回null,即出现错误
            return null;
        }
    }).onErrorReturn(new Function<Throwable, Integer>() {

        @Override
        public Integer apply(Throwable throwable) throws Exception {
            // 出现错误时,用一个默认的数据项将其替代,这里根据不同的错误返回不同的数据项
            return 100;
        }
    }).subscribe(new BaseObserver<Integer>() {

        @Override
        public void onError(@NotNull ApiException e) {
        }

        @Override
        public void onSuccess(Integer integer) {
        }
    });
    // onErrorResumeNext: 在遇到错误时开始发射第二个Observable的数据序列
    Observable.fromCallable(new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            // 返回null,即出现错误
            return null;
        }
    }).onErrorResumeNext(new Function<Throwable, ObservableSource<? extends Integer>>() {

        @Override
        public ObservableSource<? extends Integer> apply(Throwable throwable) throws Exception {
            // 出现错误时开始发射新的Observable的数据序列
            return Observable.just(1, 2, 3);
        }
    }).subscribe(new BaseObserver<Integer>() {

        @Override
        public void onError(@NotNull ApiException e) {
        }

        @Override
        public void onSuccess(Integer integer) {
        }
    });
    // 实例
    Observable observable1 = Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> e) throws Exception {
            Log.d(TAG, "1");
            e.onNext(1);
            e.onComplete();
        }
    });
    // observable2出现错误时不应该影响整个流程,所以使用Observable.empty()让这条数据源正常结束
    Observable observable2 = Observable.fromCallable(new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            // 返回null,即出现错误
            return null;
        }
    });
    Observable observable3 = Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> e) throws Exception {
            Log.d(TAG, "2");
            e.onNext(2);
            e.onComplete();
        }
    });
    Observable.concatArrayDelayError(observable1, observable2.onErrorResumeNext(Observable.empty()), observable3).subscribe(new BaseObserver<Integer>() {

        @Override
        public void onError(@NotNull ApiException e) {
        }

        @Override
        public void onSuccess(Integer integer) {
            Log.d(TAG, "onSuccess" + integer);
        }
    });
}
Also used : BiFunction(io.reactivex.functions.BiFunction) Function(io.reactivex.functions.Function) ApiException(com.androidwind.androidquick.module.exception.ApiException) IOException(java.io.IOException) Observable(io.reactivex.Observable) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Aggregations

ApiException (com.androidwind.androidquick.module.exception.ApiException)8 TestApis (com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis)4 BiFunction (io.reactivex.functions.BiFunction)3 Function (io.reactivex.functions.Function)2 List (java.util.List)2 View (android.view.View)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 BindView (butterknife.BindView)1 NameBean (com.androidwind.androidquick.demo.bean.NameBean)1 TestBean (com.androidwind.androidquick.demo.bean.TestBean)1 Gank2Apis (com.androidwind.androidquick.demo.features.module.network.retrofit.Gank2Apis)1 GankRes (com.androidwind.androidquick.demo.features.module.network.retrofit.GankRes)1 CommonViewHolder (com.androidwind.androidquick.ui.adapter.CommonViewHolder)1 Observable (io.reactivex.Observable)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 OkHttpClient (okhttp3.OkHttpClient)1 Retrofit (retrofit2.Retrofit)1