Retrofit Periodic call with Pagination

为君一笑 提交于 2019-12-24 01:15:08

问题


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
                    }
                });

     }

As you can see i have incremented the current_page by 1 every time on SuccessFull 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.


回答1:


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");
                        }
                    }
                });

     }


来源:https://stackoverflow.com/questions/37993484/retrofit-periodic-call-with-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!