Search in sources :

Example 61 with Subscription

use of rx.Subscription in project GeekNews by codeestX.

the class TechPresenter method getSearchTechData.

private void getSearchTechData() {
    currentPage = 1;
    Subscription rxSubscription = mRetrofitHelper.fetchGankSearchList(queryStr, currentTech, NUM_OF_PAGE, currentPage).compose(RxUtil.<GankHttpResponse<List<GankSearchItemBean>>>rxSchedulerHelper()).compose(RxUtil.<List<GankSearchItemBean>>handleResult()).map(new Func1<List<GankSearchItemBean>, List<GankItemBean>>() {

        @Override
        public List<GankItemBean> call(List<GankSearchItemBean> gankSearchItemBeen) {
            List<GankItemBean> newList = new ArrayList<>();
            for (GankSearchItemBean item : gankSearchItemBeen) {
                GankItemBean bean = new GankItemBean();
                bean.set_id(item.getGanhuo_id());
                bean.setDesc(item.getDesc());
                bean.setPublishedAt(item.getPublishedAt());
                bean.setWho(item.getWho());
                bean.setUrl(item.getUrl());
                newList.add(bean);
            }
            return newList;
        }
    }).subscribe(new CommonSubscriber<List<GankItemBean>>(mView) {

        @Override
        public void onNext(List<GankItemBean> gankItemBeen) {
            mView.showContent(gankItemBeen);
        }
    });
    addSubscrebe(rxSubscription);
}
Also used : GankHttpResponse(com.codeest.geeknews.model.http.response.GankHttpResponse) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(rx.Subscription) Func1(rx.functions.Func1) GankItemBean(com.codeest.geeknews.model.bean.GankItemBean) GankSearchItemBean(com.codeest.geeknews.model.bean.GankSearchItemBean)

Example 62 with Subscription

use of rx.Subscription in project okhttp-OkGo by jeasonlzy.

the class RxBitmapActivity method requestImage.

@OnClick(R.id.requestImage)
public void requestImage(View view) {
    Subscription subscription = //
    ServerApi.getBitmap("aaa", "bbb").doOnSubscribe(new Action0() {

        @Override
        public void call() {
            showLoading();
        }
    }).observeOn(//
    AndroidSchedulers.mainThread()).subscribe(new Action1<Bitmap>() {

        @Override
        public void call(Bitmap bitmap) {
            //请求成功
            dismissLoading();
            handleResponse(bitmap, null, null);
            imageView.setImageBitmap(bitmap);
            System.out.println("---------");
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            //请求失败
            throwable.printStackTrace();
            showToast("请求失败");
            dismissLoading();
            handleError(null, null);
        }
    });
    addSubscribe(subscription);
}
Also used : Action0(rx.functions.Action0) Bitmap(android.graphics.Bitmap) Subscription(rx.Subscription) OnClick(butterknife.OnClick)

Example 63 with Subscription

use of rx.Subscription in project okhttp-OkGo by jeasonlzy.

the class RxCommonActivity method upString.

@OnClick(R.id.upString)
public void upString(View view) {
    Subscription subscription = //
    OkGo.post(Urls.URL_TEXT_UPLOAD).headers("bbb", //
    "222").upString(//
    "上传的文本。。。").getCall(StringConvert.create(), //以上为产生请求事件,请求默认发生在IO线程
    RxAdapter.<String>create()).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            //开始请求前显示对话框
            showLoading();
        }
    }).observeOn(//切换到主线程
    AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            //请求成功,关闭对话框
            dismissLoading();
            handleResponse(s, null, null);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
            //请求失败
            dismissLoading();
            showToast("请求失败");
            handleError(null, null);
        }
    });
    addSubscribe(subscription);
}
Also used : Action0(rx.functions.Action0) Subscription(rx.Subscription) OnClick(butterknife.OnClick)

Example 64 with Subscription

use of rx.Subscription in project okhttp-OkGo by jeasonlzy.

the class RxCommonActivity method upJson.

@OnClick(R.id.upJson)
public void upJson(View view) {
    HashMap<String, String> params = new HashMap<>();
    params.put("key1", "value1");
    params.put("key2", "这里是需要提交的json格式数据");
    params.put("key3", "也可以使用三方工具将对象转成json字符串");
    params.put("key4", "其实你怎么高兴怎么写都行");
    JSONObject jsonObject = new JSONObject(params);
    Subscription subscription = //
    OkGo.post(Urls.URL_TEXT_UPLOAD).headers("bbb", //
    "222").upJson(//
    jsonObject.toString()).getCall(StringConvert.create(), //以上为产生请求事件,请求默认发生在IO线程
    RxAdapter.<String>create()).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            //开始请求前显示对话框
            showLoading();
        }
    }).observeOn(//切换到主线程
    AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            //请求成功,关闭对话框
            dismissLoading();
            handleResponse(s, null, null);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
            //请求失败
            dismissLoading();
            showToast("请求失败");
            handleError(null, null);
        }
    });
    addSubscribe(subscription);
}
Also used : Action0(rx.functions.Action0) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Subscription(rx.Subscription) OnClick(butterknife.OnClick)

Example 65 with Subscription

use of rx.Subscription in project okhttp-OkGo by jeasonlzy.

the class RxCommonActivity method jsonArrayRequest.

@OnClick(R.id.jsonArrayRequest)
public void jsonArrayRequest(View view) {
    Subscription subscription = //
    ServerApi.getServerListModel("aaa", "bbb").doOnSubscribe(new Action0() {

        @Override
        public void call() {
            showLoading();
        }
    }).map(new Func1<LzyResponse<List<ServerModel>>, List<ServerModel>>() {

        @Override
        public List<ServerModel> call(LzyResponse<List<ServerModel>> response) {
            return response.data;
        }
    }).observeOn(//
    AndroidSchedulers.mainThread()).subscribe(new Action1<List<ServerModel>>() {

        @Override
        public void call(List<ServerModel> serverModels) {
            //请求成功
            dismissLoading();
            handleResponse(serverModels, null, null);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            //请求失败
            throwable.printStackTrace();
            showToast("请求失败");
            dismissLoading();
            handleError(null, null);
        }
    });
    addSubscribe(subscription);
}
Also used : Action0(rx.functions.Action0) LzyResponse(com.lzy.demo.model.LzyResponse) ServerModel(com.lzy.demo.model.ServerModel) List(java.util.List) Subscription(rx.Subscription) OnClick(butterknife.OnClick)

Aggregations

Subscription (rx.Subscription)302 Test (org.junit.Test)82 List (java.util.List)75 CompositeSubscription (rx.subscriptions.CompositeSubscription)55 Action0 (rx.functions.Action0)45 ArrayList (java.util.ArrayList)41 CountDownLatch (java.util.concurrent.CountDownLatch)38 Func1 (rx.functions.Func1)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 Observable (rx.Observable)17 LinkedList (java.util.LinkedList)13 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)11 PlayList (io.github.ryanhoo.music.data.model.PlayList)11 MetaChangedEvent (io.hefuyi.listener.event.MetaChangedEvent)10 View (android.view.View)9 IOException (java.io.IOException)9 AndroidSchedulers (rx.android.schedulers.AndroidSchedulers)8 Intent (android.content.Intent)7