如何在Retrofit with Android中使用特定查询?

时间:2018-11-20 09:16:31

标签: android retrofit retrofit2 android-paging android-paging-library

我有一个API接口,其中有两个重载方法:

public interface Api {
    @GET("movie/now_playing")
    Call<ApiResponse> getMovies(@Query("page") int page);

    @GET("search/movie")
    Call<ApiResponse> getMovies(@Query("query") String query,  @Query("page") int page);
}

第一个用于现在播放电影,第二个用于搜索电影。

在我的MainActivity中,我使用以下代码在RecyclerView中显示数据:

MovieViewModel movieViewModel = ViewModelProviders.of(this).get(MovieViewModel.class);
MovieAdapter adapter = new MovieAdapter(this);
movieViewModel.moviePagedList.observe(this, adapter::submitList);
recyclerView.setAdapter(adapter);

这是我的MovieViewModel班:

public class MovieViewModel extends ViewModel {
    LiveData<PagedList<ApiResponse.Movie>> moviePagedList;

    public MovieViewModel() {
        MovieDataSourceFactory movieDataSourceFactory = new MovieDataSourceFactory();
        PagedList.Config config = new PagedList.Config.Builder().setEnablePlaceholders(false).setPageSize(20).build();
        moviePagedList = new LivePagedListBuilder<>(movieDataSourceFactory, config).build();
    }
}

这是我的MovieDataSourceFactory类:

public class MovieDataSourceFactory extends DataSource.Factory<Integer, ApiResponse.Movie> {
    private MutableLiveData<PageKeyedDataSource<Integer, ApiResponse.Movie>> movieLiveDataSource = new MutableLiveData<>();

    @Override
    public DataSource<Integer, ApiResponse.Movie> create() {
        MovieDataSource movieDataSource = new MovieDataSource();
        movieLiveDataSource.postValue(movieDataSource);
        return movieDataSource;
    }
}

这是我的MovieDataSource班:

public class MovieDataSource extends PageKeyedDataSource<Integer, ApiResponse.Movie> {
    @Override
    public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, ApiResponse.Movie> callback) {
        Api api = RetrofitClient.getInstance().getApi();
        Callback<ApiResponse> call = new Callback<ApiResponse>() {
            @Override
            public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
                if(response.body() != null){
                    callback.onResult(response.body().movieList, null, page + 1);
                }
            }

            @Override
            public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {}
        };
        api.getMovies(page).enqueue(call);
    }

    //loadBefore and loadAfter methods
}

如果运行此代码,一切正常,调用第一个查询,然后正确获取结果。问题是,我该如何动态使用其中一个?

api.getMovies(query, page).enqueue(call);

1 个答案:

答案 0 :(得分:0)

boolean globalVariableShouldUseGetMoviesCallWithOnlyOneArgument = true

private void doNetworkCallAndCheckBooleanStatus(){ 
    if (globalVariableShouldUseGetMoviesCallWithOnlyOneArgument) {
        getMovies(thisIsOneArgumentAsYouCanSee);
    } else {
        getMovies(thisIsUsingTwoArguments, asYouCanSee);
    }
}

private void someOtherMethodInTheApp() {
    globalVariableShouldUseGetMoviesCallWithOnlyOneArgument = false
    doNetworkCallAndCheckBooleanStatus()
}