Skip to content

Chained

D edited this page Feb 12, 2019 · 3 revisions

Chained form 3-1: Simple callback

        // 3-1-1 SimpleCallback
        Aster.getDefault().get(url, params)
                .request(new SimpleCallback<MovieInfo>() {
                    @Override
                    public void onSuccess(MovieInfo info) {
                        ...do something in main thread
                    }

                    @Override
                    public void onError(Throwable e) {
                        ...do something in main thread
                    }
                });

        -- Or --

        // 3-1-2 AsyncCallback
        Aster.getDefault().get(url, params)
                .request(new AsyncCallback<MovieInfo, String>() {
                    @Override
                    public String apply(@NonNull MovieInfo info) throws Exception {
                        ...Success step 1-1 -->
                        ...do something in asynchronous thread, such as time-consuming, map conversion, etc.
                        int size = info.subjects.size();
                        return "" + size;
                    }

                    @Override
                    public void onSuccess(String response) {
                        ...Success step 1-2 -->
                        ...do something in main thread
                    }

                    @Override
                    public void onError(Throwable e) {
                        ...Error -->
                        ...do something in main thread
                    }
                });

Chained form 3-2: .observable(T) specifies a generic T-specific return type, calls Retrofit's observer instead of the Callback interface

        Aster.getDefault().get(url, params)
                .observable(MovieInfo.class)
                .map(new Function<MovieInfo, MovieInfo>() {
                    @Override
                    public MovieInfo apply(@NonNull MovieInfo info) throws Exception {
                        return info;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<MovieInfo>() {
                    @Override
                    public void onNext(@NonNull MovieInfo info) {
                        ...
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        ...
                    }

                    @Override
                    public void onComplete() {
                        ...
                    }
                });

Chained form 3-3: Aster.getAster().getRetrofit() gets Retrofit, fully customizable .create()

        Aster.getAster().getRetrofit().create(SubAPI.class)
                .get(url)
                .subscribeOn(Schedulers.io())
                .map(new Function<ResponseBody, ArrayList<Boolean>>() {
                    @Override
                    public ArrayList<Boolean> apply(@NonNull ResponseBody info) throws Exception {
                        return new ArrayList<>();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<ArrayList<Boolean>>() {
                    @Override
                    public void onNext(ArrayList<Boolean> booleans) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });