简体   繁体   English

Android 分页库中的 loadAfter() 从 API 无限加载第一页

[英]loadAfter() in Android paging library infinitely loading the first page from API

I'm implementing the paging library 2.1.2 in my Android app, my DataSource class looks like this我正在我的 Android 应用程序中实现分页库 2.1.2,我的数据源 class 看起来像这样

public class TransactionsDataSource extends PageKeyedDataSource<Integer, Items> {


private static final int FIRST_PAGE = 1;
private Context context;
private Repository repository;
private String bearerToken;
private int merchantId;


public TransactionsDataSource(Context context, int merchantId) {
    this.context = context;
    this.merchantId = merchantId;
    repository = new Repository();
    repository.initLoginSharedPref(context);
    bearerToken = repository.getAccessToken();
}


@Override
public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<Integer> params, @NonNull PageKeyedDataSource.LoadInitialCallback<Integer, Items> callback) {


    ApiClient.getApiClient().getApiInterface().getMerchantTransactions(bearerToken,
            merchantId, FIRST_PAGE)
            .enqueue(new Callback<MainResponse>() {
                @Override
                public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
                    if (response.body() != null){

                        int code = response.body().getCode();
                        if (code == SERVER_OK_CODE){
                            callback.onResult(response.body().getData().getItems(), null, FIRST_PAGE + 1);
                        } else if (code == SERVER_UNAUTHENTICATED_CODE){

                            repository.clearCache();
                            HelperMethods.signOut(context);
                        } else {
                            //no more data to load
                        }


                    }
                }

                @Override
                public void onFailure(Call<MainResponse> call, Throwable t) {

                }
            });
}

@Override
public void loadBefore(@NonNull PageKeyedDataSource.LoadParams<Integer> params, @NonNull PageKeyedDataSource.LoadCallback<Integer, Items> callback) {

}

@Override
public void loadAfter(@NonNull PageKeyedDataSource.LoadParams<Integer> params, @NonNull PageKeyedDataSource.LoadCallback<Integer, Items> callback) {


    ApiClient.getApiClient().getApiInterface().getMerchantTransactions(bearerToken,
            merchantId, params.key)
            .enqueue(new Callback<MainResponse>() {
                @Override
                public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
                    if (response.body() != null){

                        int code = response.body().getCode();
                        if (code == SERVER_OK_CODE){

                            Integer key = params.key + 1;
                            callback.onResult(response.body().getData().getItems(), key);

                        } else if (code == SERVER_UNAUTHENTICATED_CODE){

                            repository.clearCache();
                            HelperMethods.signOut(context);
                        } else {
                            //no more data to load
                        }

                    }
                }

                @Override
                public void onFailure(Call<MainResponse> call, Throwable t) {

                }
            });
}

} }

As per my knowledge the loadInitial() method is reponsible for loading the first page of data and then loadAfter() will be triggered after that to load the rest of the pages while the key is incremented by one until there's no more data to be loaded.据我所知,loadInitial() 方法负责加载第一页数据,然后将触发 loadAfter() 以加载页面的 rest,同时键递增一个,直到没有更多数据要加载. But when I debug the app the loadAfter() is continously repeat loading the first page only even I make sure to increment the params.key() by 1. I even tried to hard code the page number as following but still loading the first page only但是当我调试应用程序时,即使我确保将 params.key() 递增 1,loadAfter() 也会不断重复加载第一页。我什至尝试如下硬编码页码,但仍在加载第一页仅有的

ApiClient.getApiClient().getApiInterface().getMerchantTransactions(bearerToken,
        merchantId, 2)

I need to know what's happening and how to fix this bug?我需要知道发生了什么以及如何修复这个错误?

Although my implementation is 100% correct and After I tried so many things finally I got my way to solve this weird behavior.尽管我的实现是 100% 正确的,而且在我尝试了很多事情之后,我终于找到了解决这种奇怪行为的方法。 I made a small change in my interface instead of passing the page number to getMerchantTransactions() I decided to send the whole url as a parameter and it worked fine here's my method in interface after update我在我的界面中做了一个小改动,而不是将页码传递给 getMerchantTransactions() 我决定将整个 url 作为参数发送,它工作正常这是我在更新后界面中的方法

@POST
@FormUrlEncoded
Call<MainResponse> getMerchantTransactions(@Header("Authorization") String bearerToken,
                                           @Field("id") int merchantId,
                                           @Url String url);

And then calling this method in loadInitial() and loadAfter() like this然后像这样在 loadInitial() 和 loadAfter() 中调用这个方法

ApiClient.getApiClient().getApiInterface().getMerchantTransactions(bearerToken,
            merchantId, "myUrl?pagenumber=" + params.key)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM