本文介绍了使用分页改造定期调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Retrofit2.0 轮询服务器.我在 x 秒内得到结果,但问题是 API 中的页码没有更新.让我们来看看代码以获得更好的说明

I am currently using the Retrofit2.0 to poll the server .I am getting the result in x second but the problem is page number is not updating in the API.Lets come to the code for better clarification

private void startPolling() throws Exception {
        Log.e("APP CONSTANT","" + current_page);
        MrSaferWebService service =  ServiceFactory.createRetrofitService(MrSaferWebService.class,  AppConstants.BASE_URL);
        final Observable<ReportResponse> reportResponseObservable =  service.getListOfInciden("get_report", current_page, 5, incident_lat,  incident_long);
        Observable.interval(0,20,TimeUnit.SECONDS)
                .flatMap(new Func1<Long, Observable<ReportResponse>> () {
                    @Override
                    public Observable<ReportResponse> call(Long  aLong) {
                        return reportResponseObservable;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ReportResponse>() {
                    @Override
                    public void call(ReportResponse response) {
                        Log.i("HEARTBEAT_INTERVAL", "Response from HEARTBEAT");
                        ActivityUtils.showProgress(false, mRootView, mProgressView, mContext);
                        if (response.getStatus() == 1) {
                            current_page = current_page + 1;
                             if (!response.getReportList().isEmpty()) {
                                addItems(response.getReportList());
                           }
                             else{
                                //do nothing
                            }

                        } else {
                            Log.e("MY ERROR", "" + "SOME ERROR OCCURED");
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        ActivityUtils.showProgress(true, mRootView, mProgressView, mContext);
                        // TODO: 22/03/16 ADD ERROR HANDLING
                    }
                });

     }

如您所见,我每次都将 current_page 增加 1SuccessFull Response 但是当我检查日志时,current_page 值仅增加一次,之后该日志不存在,因此值也没有增加..所以它每次都采用相同的页码并给我重复的响应.

As you can see i have incremented the current_page by 1 every time onSuccessFull Response but when i check the Log the current_page value are increased only once and after that log are not there and hence there value is also not increasing..So it taking the the same page number every time and giving me the Duplicate response.

请帮我找到我遗漏的东西.

Please help me to find what i am missing.

推荐答案

在花了一天多的时间后,我刚刚更改了 Action with Subscriber,一切似乎都在工作.我不知道内部发生了什么,但它有效.我仍在试图弄清楚 Action 和 Subscriber 之间的区别.下面是我更新的代码,它做了一些技巧.

After spending more than a day i just changed Action with Subscriber and everything seems to be working .I don't know what happen internally but it works . I am still trying to figure it out what the difference between Action and Subscriber.Below are my updated code which did the tricks.

private void startPolling() throws Exception {
        final MrSaferWebService service =  ServiceFactory.createRetrofitService(MrSaferWebService.class,  AppConstants.BASE_URL);
        Observable
                .interval(0,20,TimeUnit.SECONDS)
                .flatMap(new Func1<Long, Observable<ReportResponse>>() {
                    @Override
                    public Observable<ReportResponse> call(Long aLong) {
                        Log.e("PAGE", "" + current_page);
                        return service.getListOfInciden("get_report", current_page, 5, incident_lat, incident_long);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ReportResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (mProgressView !=null &&  mRootView !=null) {
                            ActivityUtils.showProgress(false, mRootView, mProgressView, mContext);
                        }

                    }

                    @Override
                    public void onNext(ReportResponse response) {
                        if (mProgressView !=null &&  mRootView !=null) {
                            ActivityUtils.showProgress(false, mRootView, mProgressView, mContext);
                        }
                        if (response.getStatus() == 1) {
                            if (!response.getReportList().isEmpty()){
                                current_page = current_page + 1;
                                addItems(response.getReportList());
                            }
                            else{
                                //do nothing
                            }

                        } else {
                            Log.e("MY ERROR", "" + "SOME ERROR OCCURED");
                        }
                    }
                });

     }

这篇关于使用分页改造定期调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:02