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();
}
});
}
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("下载完成");
}
});
}
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();
}
});
}
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();
}
});
}
Aggregations