【发布时间】:2021-11-10 12:58:15
【问题描述】:
我正在我的 Android 应用中实现分页库 2.1.2,我的 DataSource 类看起来像这样
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) {
}
});
}
}
据我所知,loadInitial() 方法负责加载第一页数据,然后将触发 loadAfter() 以加载其余页面,同时键递增 1 直到没有更多数据要加载。但是当我调试应用程序时,即使我确保将 params.key() 增加 1,loadAfter() 也会不断重复加载第一页。我什至尝试将页码硬编码如下,但仍然加载第一页只有
ApiClient.getApiClient().getApiInterface().getMerchantTransactions(bearerToken,
merchantId, 2)
我需要知道发生了什么以及如何修复此错误?
【问题讨论】:
-
出于好奇,您是否有任何理由使用分页 2.x API?在最新的稳定版分页中,大部分 api 表面已被弃用。
-
我还想问一下,您能否发布一下您发出的 HTTP 调用和返回的响应是什么?如果您请求的页面不存在,您的后端是否有可能返回上一页?例如,只有一页数据?我在您的代码中立即看到的一个问题是您永远不会终止(将 null 传递给 nextKey)到
PageKeyedDataSource.LoadCallback。 -
@dlam 让我向您澄清一下,1- 在调试期间,我确保 loadAfter 从 API 调用第一页,而在 PostMan 上,API 工作正常。 2-你说我的代码永远不会终止,这是不正确的,因为有一个 if 条件,我检查 if (code == SERVER_UNAUTHENTICATED_CODE) 这意味着响应携带新数据,否则它将终止
-
我的意思是,您应该通过为 nextKey 返回 null 来与 paging 交流该页面是终端(最后一页),否则它将继续尝试朝该方向加载。
标签: java android retrofit2 android-paging android-paging-library