Search in sources :

Example 36 with NonNull

use of io.reactivex.annotations.NonNull in project RxJavaSamples by rengwuxian.

the class ZipFragment method load.

@OnClick(R.id.zipLoadBt)
void load() {
    swipeRefreshLayout.setRefreshing(true);
    unsubscribe();
    disposable = Observable.zip(Network.getGankApi().getBeauties(200, 1).map(GankBeautyResultToItemsMapper.getInstance()), Network.getZhuangbiApi().search("装逼"), new BiFunction<List<Item>, List<ZhuangbiImage>, List<Item>>() {

        @Override
        public List<Item> apply(List<Item> gankItems, List<ZhuangbiImage> zhuangbiImages) {
            List<Item> items = new ArrayList<Item>();
            for (int i = 0; i < gankItems.size() / 2 && i < zhuangbiImages.size(); i++) {
                items.add(gankItems.get(i * 2));
                items.add(gankItems.get(i * 2 + 1));
                Item zhuangbiItem = new Item();
                ZhuangbiImage zhuangbiImage = zhuangbiImages.get(i);
                zhuangbiItem.description = zhuangbiImage.description;
                zhuangbiItem.imageUrl = zhuangbiImage.image_url;
                items.add(zhuangbiItem);
            }
            return items;
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<List<Item>>() {

        @Override
        public void accept(@NonNull List<Item> items) throws Exception {
            swipeRefreshLayout.setRefreshing(false);
            adapter.setItems(items);
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(@NonNull Throwable throwable) throws Exception {
            swipeRefreshLayout.setRefreshing(false);
            Toast.makeText(getActivity(), R.string.loading_failed, Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : Item(com.rengwuxian.rxjavasamples.model.Item) Consumer(io.reactivex.functions.Consumer) NonNull(io.reactivex.annotations.NonNull) ArrayList(java.util.ArrayList) List(java.util.List) ZhuangbiImage(com.rengwuxian.rxjavasamples.model.ZhuangbiImage) OnClick(butterknife.OnClick)

Example 37 with NonNull

use of io.reactivex.annotations.NonNull in project okhttp-OkGo by jeasonlzy.

the class RxFileDownloadActivity method fileDownload2.

@OnClick(R.id.fileDownload2)
public void fileDownload2(View view) {
    Observable.create(new ObservableOnSubscribe<Progress>() {

        @Override
        public void subscribe(@NonNull final ObservableEmitter<Progress> e) throws Exception {
            String etString = etUrl.getText().toString();
            String url = TextUtils.isEmpty(etString) ? Urls.URL_DOWNLOAD : etString;
            // 
            OkGo.<File>get(url).headers("aaa", // 
            "111").params("bbb", // 
            "222").execute(new FileCallback() {

                @Override
                public void onSuccess(Response<File> response) {
                    e.onComplete();
                }

                @Override
                public void onError(Response<File> response) {
                    e.onError(response.getException());
                }

                @Override
                public void downloadProgress(Progress progress) {
                    e.onNext(progress);
                }
            });
        }
    }).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(@NonNull Disposable disposable) throws Exception {
            btnFileDownload2.setText("正在下载中...");
        }
    }).observeOn(// 
    AndroidSchedulers.mainThread()).subscribe(new Observer<Progress>() {

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            addDisposable(d);
        }

        @Override
        public void onNext(@NonNull Progress progress) {
            String downloadLength = Formatter.formatFileSize(getApplicationContext(), progress.currentSize);
            String totalLength = Formatter.formatFileSize(getApplicationContext(), progress.totalSize);
            tvDownloadSize.setText(downloadLength + "/" + totalLength);
            String speed = Formatter.formatFileSize(getApplicationContext(), progress.speed);
            tvNetSpeed.setText(String.format("%s/s", speed));
            tvProgress.setText(numberFormat.format(progress.fraction));
            pbProgress.setMax(10000);
            pbProgress.setProgress((int) (progress.fraction * 10000));
        }

        @Override
        public void onError(@NonNull Throwable e) {
            e.printStackTrace();
            btnFileDownload2.setText("下载出错");
            showToast(e.getMessage());
        }

        @Override
        public void onComplete() {
            btnFileDownload2.setText("下载完成");
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) Progress(com.lzy.okgo.model.Progress) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) FileCallback(com.lzy.okgo.callback.FileCallback) NonNull(io.reactivex.annotations.NonNull) File(java.io.File) ObservableEmitter(io.reactivex.ObservableEmitter) OnClick(butterknife.OnClick)

Example 38 with NonNull

use of io.reactivex.annotations.NonNull in project okhttp-OkGo by jeasonlzy.

the class RxRetrofitActivity method jsonArrayRequest.

@OnClick(R.id.jsonArrayRequest)
public void jsonArrayRequest(View view) {
    Type type = new TypeToken<LzyResponse<List<ServerModel>>>() {
    }.getType();
    // 
    ServerApi.<LzyResponse<List<ServerModel>>>getData(type, Urls.URL_JSONARRAY, "aaa", "bbb").doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(@NonNull Disposable disposable) throws Exception {
            showLoading();
        }
    }).map(new Function<LzyResponse<List<ServerModel>>, List<ServerModel>>() {

        @Override
        public List<ServerModel> apply(@NonNull LzyResponse<List<ServerModel>> response) throws Exception {
            return response.data;
        }
    }).observeOn(// 
    AndroidSchedulers.mainThread()).subscribe(new Observer<List<ServerModel>>() {

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            addDisposable(d);
        }

        @Override
        public void onNext(@NonNull List<ServerModel> response) {
            handleResponse(response);
        }

        @Override
        public void onError(@NonNull Throwable e) {
            // 请求失败
            e.printStackTrace();
            showToast("请求失败");
            handleError(null);
        }

        @Override
        public void onComplete() {
            dismissLoading();
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) LzyResponse(com.lzy.demo.model.LzyResponse) Type(java.lang.reflect.Type) ServerModel(com.lzy.demo.model.ServerModel) Consumer(io.reactivex.functions.Consumer) NonNull(io.reactivex.annotations.NonNull) List(java.util.List) OnClick(butterknife.OnClick)

Example 39 with NonNull

use of io.reactivex.annotations.NonNull in project okhttp-OkGo by jeasonlzy.

the class RxRetrofitActivity method jsonRequest.

@OnClick(R.id.jsonRequest)
public void jsonRequest(View view) {
    Type type = new TypeToken<LzyResponse<ServerModel>>() {
    }.getType();
    // 
    ServerApi.<LzyResponse<ServerModel>>getData(type, Urls.URL_JSONOBJECT, "aaa", "bbb").subscribeOn(// 
    Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(@NonNull Disposable disposable) throws Exception {
            showLoading();
        }
    }).map(new Function<LzyResponse<ServerModel>, ServerModel>() {

        @Override
        public ServerModel apply(@NonNull LzyResponse<ServerModel> response) throws Exception {
            return response.data;
        }
    }).observeOn(// 
    AndroidSchedulers.mainThread()).subscribe(new Observer<ServerModel>() {

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            addDisposable(d);
        }

        @Override
        public void onNext(@NonNull ServerModel response) {
            handleResponse(response);
        }

        @Override
        public void onError(@NonNull Throwable e) {
            // 请求失败
            e.printStackTrace();
            showToast("请求失败");
            handleError(null);
        }

        @Override
        public void onComplete() {
            dismissLoading();
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) LzyResponse(com.lzy.demo.model.LzyResponse) Type(java.lang.reflect.Type) ServerModel(com.lzy.demo.model.ServerModel) Consumer(io.reactivex.functions.Consumer) NonNull(io.reactivex.annotations.NonNull) OnClick(butterknife.OnClick)

Aggregations

NonNull (io.reactivex.annotations.NonNull)39 Disposable (io.reactivex.disposables.Disposable)17 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)15 Consumer (io.reactivex.functions.Consumer)14 Function (io.reactivex.functions.Function)13 BuildDetails (com.khmelenko.lab.varis.network.response.BuildDetails)6 OnClick (butterknife.OnClick)5 List (java.util.List)5 RequestBody (okhttp3.RequestBody)5 ObservableEmitter (io.reactivex.ObservableEmitter)4 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Headers (okhttp3.Headers)3 View (android.view.View)2 ANError (com.androidnetworking.error.ANError)2 User (com.khmelenko.lab.varis.network.response.User)2 LzyResponse (com.lzy.demo.model.LzyResponse)2 ServerModel (com.lzy.demo.model.ServerModel)2 Action (io.reactivex.functions.Action)2