Android 用Mvvm方法处理分页库中的错误

Android 用Mvvm方法处理分页库中的错误,android,mvvm,error-handling,android-paging-library,Android,Mvvm,Error Handling,Android Paging Library,我刚开始使用分页库,代码工作正常,但我不知道如何处理错误,比如进度条和Toast错误消息,所以我的代码就在这里 这是回购协议 public class SearchRepository extends PageKeyedDataSource<Integer, Doc> { public static final int PAGE_SIZE = 5; private static final int FIRST_PAGE = 1; private static final String

我刚开始使用分页库,代码工作正常,但我不知道如何处理错误,比如进度条和Toast错误消息,所以我的代码就在这里 这是回购协议

public class SearchRepository extends PageKeyedDataSource<Integer, Doc> {
public static final int PAGE_SIZE = 5;
private static final int FIRST_PAGE = 1;
private static final String token = "1234";
private static final String TAG = "www";

public SearchRepository() {
}

@Override
public void loadInitial(@NonNull final LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<Integer, Doc> callback) {
    Log.e(TAG, "loadInitial: ");
    APIService apiService = RetroClass2.getAPIService();
    apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, FIRST_PAGE, "")
            .enqueue(new Callback<Products>() {
                @Override
                public void onResponse(Call<Products> call, Response<Products> response) {
                    if (response.body() != null) {

                        callback.onResult(response.body().getResult().getDocs(), null, FIRST_PAGE + 1);
                    }
                }

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

                }
            });
}

@Override
public void loadBefore(@NonNull final LoadParams<Integer> params, @NonNull final LoadCallback<Integer, Doc> callback) {
    Log.e(TAG, "loadBefore: ");
    APIService apiService = RetroClass2.getAPIService();
    apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, params.key, "")
            .enqueue(new Callback<Products>() {
                @Override
                public void onResponse(Call<Products> call, Response<Products> response) {
                    if (response.body() != null) {
                        Integer key = (params.key > 1) ? params.key - 1 : null;
                        callback.onResult(response.body().getResult().getDocs(), key);

                    }
                }

                @Override
                public void onFailure(Call<Products> call, Throwable t) {
                }
            });
}

@Override
public void loadAfter(@NonNull final LoadParams<Integer> params, @NonNull final LoadCallback<Integer, Doc> callback) {
    Log.e(TAG, "loadAfter: ");
    APIService apiService = RetroClass2.getAPIService();
    apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, params.key, "")
            .enqueue(new Callback<Products>() {
                @Override
                public void onResponse(Call<Products> call, Response<Products> response) {
                    if (response.body() != null) {

                        Integer key = response.body().getResult().getDocs().size() != 0 ? params.key + 1 : null;
                        callback.onResult(response.body().getResult().getDocs(), key);
                    }
                }

                @Override
                public void onFailure(Call<Products> call, Throwable t) {
                }
            });
}
公共类SearchRepository扩展了PageKeyedDataSource{
公共静态最终整版页面大小=5;
专用静态最终int第一页=1;
私有静态最终字符串标记=“1234”;
私有静态最终字符串TAG=“www”;
公共搜索存储库(){
}
@凌驾
public void loadInitial(@NonNull final LoadInitialParams params,@NonNull final LoadInitialCallback){
Log.e(标签“loadInitial:”);
APIService APIService=RetroClass2.getAPIService();
SeachPro(标记,“名称”,“升序”,页面大小,第一页,”)
.enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
if(response.body()!=null){
callback.onResult(response.body().getResult().getDocs(),null,第一页+1);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}
@凌驾
public void loadBefore(@NonNull final LoadParams params,@NonNull final LoadCallback){
Log.e(标签“loadBefore:”);
APIService APIService=RetroClass2.getAPIService();
apiService.SeachPro(标记,“名称”,“升序”,页面大小,参数键“”)
.enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
if(response.body()!=null){
整数key=(params.key>1)?params.key-1:null;
onResult(response.body().getResult().getDocs(),key);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}
@凌驾
public void loadAfter(@NonNull final LoadParams params,@NonNull final LoadCallback callback){
Log.e(标签“loadAfter:”);
APIService APIService=RetroClass2.getAPIService();
apiService.SeachPro(标记,“名称”,“升序”,页面大小,参数键“”)
.enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
if(response.body()!=null){
整数键=response.body().getResult().getDocs().size()!=0?params.key+1:null;
onResult(response.body().getResult().getDocs(),key);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}
这是我的工厂保存的数据

public class SearchRepoFactory extends DataSource.Factory {
private MutableLiveData<PageKeyedDataSource<Integer, Doc>> listMutableLiveData = new MutableLiveData<>();


@Override
public DataSource create() {
    SearchRepository searchRepository = new SearchRepository();
    listMutableLiveData.postValue(searchRepository);
    return searchRepository;
}

public MutableLiveData<PageKeyedDataSource<Integer, Doc>> getListMutableLiveData() {
    return listMutableLiveData;
   }
}
公共类SearchRepoFactory扩展了DataSource.Factory{
私有MutableLiveData listMutableLiveData=新的MutableLiveData();
@凌驾
公共数据源创建(){
SearchRepository SearchRepository=新建SearchRepository();
listMutableLiveData.postValue(searchRepository);
返回搜索库;
}
公共MutableLiveData getListMutableLiveData(){
返回listMutableLiveData;
}
}
这是我的viewmodel

public class SearchViewModel extends ViewModel {
public LiveData<PagedList<Doc>> listLiveData;
private LiveData<PageKeyedDataSource<Integer, Doc>> liveData;
private static final String TAG = "yyy";

public SearchViewModel() {

    SearchRepoFactory factory = new SearchRepoFactory();
    liveData = factory.getListMutableLiveData();

    PagedList.Config config = (new PagedList.Config.Builder()).setEnablePlaceholders(false).setPageSize(SearchRepository.PAGE_SIZE).build();

    listLiveData = (new LivePagedListBuilder(factory, config)).build();
    }
}
公共类SearchViewModel扩展了ViewModel{
公共LiveData列表LiveData;
私有LiveData-LiveData;
私有静态最终字符串TAG=“yyy”;
公共搜索视图模型(){
SearchRepoFactory=新的SearchRepoFactory();
liveData=factory.getListMutableLiveData();
PagedList.Config Config=(新建PagedList.Config.Builder()).setEnablePlaceholders(false).setPageSize(SearchRepository.PAGE_SIZE).build();
listLiveData=(新的LivePagedListBuilder(工厂,配置)).build();
}
}
我的适配器工作正常,所以我要跳过适配器

这个活动呢

  recyclerView = findViewById(R.id.rv1);
    progressBar = findViewById(R.id.probar);
    final SearchAdapter adapter = new SearchAdapter(this);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    SearchViewModel searchViewModel = new SearchViewModel(this);

    searchViewModel.listLiveData.observe(this, new Observer<PagedList<Doc>>() {
        @Override
        public void onChanged(@Nullable PagedList<Doc> docs) {
            adapter.submitList(docs);
        }
    });
    recyclerView.setAdapter(adapter);
recyclerView=findviewbyd(R.id.rv1);
progressBar=findViewById(R.id.probar);
最终SearchAdapter=新的SearchAdapter(此);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
SearchViewModel SearchViewModel=新的SearchViewModel(此);
searchViewModel.listLiveData.observe(这是新的观察者(){
@凌驾
更改后的公共void(@Nullable PagedList docs){
适配器。提交列表(文档);
}
});
recyclerView.setAdapter(适配器);
我要再说一遍,当没有错误时,代码可以正常工作,但我想处理错误,如无连接Toast和隐藏进度条,我不想用户公共静态,我在SearchRepository中得到服务器响应,还有很长的路要走到UI,我知道我的类命名并不完美:),有什么想法吗