Search in sources :

Example 1 with ServiceFactory

use of app.main.wangliwei.enablehands.http.ServiceFactory in project EnableHands by LeviWGG.

the class NewsModel method getNewsInfo.

@Override
public void getNewsInfo(final INewsContract.INewsPresenter iNewsPresenter, int id) {
    Retrofit retrofit = new ServiceFactory().create(NewsService.BASE_URL);
    NewsService newsService = retrofit.create(NewsService.class);
    Observable<NewsInfo> observable = newsService.getNewsInfo(id);
    observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).flatMap(new Function<NewsInfo, ObservableSource<List<NewsInfo.T1348647909107Bean>>>() {

        @Override
        public ObservableSource<List<NewsInfo.T1348647909107Bean>> apply(NewsInfo newsInfo) throws Exception {
            return Observable.just(newsInfo.getT1348647909107());
        }
    }).filter(new Predicate<List<NewsInfo.T1348647909107Bean>>() {

        @Override
        public boolean test(List<NewsInfo.T1348647909107Bean> t1348647909107Beans) throws Exception {
            if (t1348647909107Beans.isEmpty()) {
                return false;
            }
            return true;
        }
    }).subscribe(new Consumer<List<NewsInfo.T1348647909107Bean>>() {

        @Override
        public void accept(List<NewsInfo.T1348647909107Bean> t1348647909107Beans) throws Exception {
            // 遍历新闻信息,去掉没有URL的子项
            for (int i = 0; i < t1348647909107Beans.size(); i++) {
                if (t1348647909107Beans.get(i).getUrl() == null) {
                    t1348647909107Beans.remove(i);
                }
            }
            iNewsPresenter.setNewsInfo(t1348647909107Beans);
        }
    });
// return (Observable<T>) observable;
}
Also used : ServiceFactory(app.main.wangliwei.enablehands.http.ServiceFactory) Predicate(io.reactivex.functions.Predicate) Retrofit(retrofit2.Retrofit) ObservableSource(io.reactivex.ObservableSource) NewsInfo(app.main.wangliwei.enablehands.bean.NewsInfo) NewsService(app.main.wangliwei.enablehands.http.NewsService) List(java.util.List)

Example 2 with ServiceFactory

use of app.main.wangliwei.enablehands.http.ServiceFactory in project EnableHands by LeviWGG.

the class PicturePresenterImp method getWeixinNews.

@Override
public void getWeixinNews() {
    Retrofit retrofit = new ServiceFactory().create(BASE_URL);
    WeixinService weixinService = retrofit.create(WeixinService.class);
    Observable<Weixin> observable = weixinService.getWeixinNews(page, 10, "json", KEY);
    observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).flatMap(new Function<Weixin, ObservableSource<List<Weixin.ResultBean.ListBean>>>() {

        @Override
        public ObservableSource<List<Weixin.ResultBean.ListBean>> apply(Weixin weixin) throws Exception {
            return Observable.just(weixin.getResult().getList());
        }
    }).filter(new Predicate<List<Weixin.ResultBean.ListBean>>() {

        @Override
        public boolean test(List<Weixin.ResultBean.ListBean> listBeans) throws Exception {
            if (listBeans.isEmpty()) {
                return false;
            }
            return true;
        }
    }).subscribe(new Consumer<List<Weixin.ResultBean.ListBean>>() {

        @Override
        public void accept(List<Weixin.ResultBean.ListBean> listBeans) throws Exception {
            if (page == 1) {
                mView.setWeixinNews(listBeans);
            } else {
                mView.loadMore(listBeans);
            }
            page++;
        }
    });
}
Also used : ServiceFactory(app.main.wangliwei.enablehands.http.ServiceFactory) Predicate(io.reactivex.functions.Predicate) Retrofit(retrofit2.Retrofit) Weixin(app.main.wangliwei.enablehands.bean.Weixin) WeixinService(app.main.wangliwei.enablehands.http.WeixinService) ObservableSource(io.reactivex.ObservableSource) List(java.util.List)

Aggregations

ServiceFactory (app.main.wangliwei.enablehands.http.ServiceFactory)2 ObservableSource (io.reactivex.ObservableSource)2 Predicate (io.reactivex.functions.Predicate)2 List (java.util.List)2 Retrofit (retrofit2.Retrofit)2 NewsInfo (app.main.wangliwei.enablehands.bean.NewsInfo)1 Weixin (app.main.wangliwei.enablehands.bean.Weixin)1 NewsService (app.main.wangliwei.enablehands.http.NewsService)1 WeixinService (app.main.wangliwei.enablehands.http.WeixinService)1