异步任务不允许我覆盖onPostExecute(方法不会覆盖它的超类方法)

时间:2016-08-13 01:22:40

标签: android android-asynctask

我正在创建一个抓取YouTube上播放列表列表的应用。它曾经是一个视频列表,但我已经更改了代码,现在它不允许我覆盖该方法。

我的理解是,我应该使用新的播放列表值而不是之前的视频来更改“扩展AsyncTask”,但它仍然不起作用。

它是:

public abstract class GetPlaylistAsyncTask extends AsyncTask<String, Void, Pair<String, List<Video>>> {

现在是:

public abstract class GetPlaylistAsyncTask extends AsyncTask<String, Void, Pair<String, List<Playlist>>> {

这就是错误:

mPlaylist = new Playlist(mPlaylistId);
initAdapter(mPlaylist);
new GetPlaylistAsyncTask(mYouTubeDataApi, mTitle, mSearchQuery) {
    @Override
    public void onPostExecute(Pair<String, List<Playlist>> result) {
        handleGetPlaylistResult(mPlaylist, result);
    }
}.execute(mPlaylist.playlistId, mPlaylist.getNextPageToken());

这就是AsyncTask:

public abstract class GetPlaylistAsyncTask extends AsyncTask<String, Void, Pair<String, List<Playlist>>> {
    private static final String TAG = "GetPlaylistAsyncTask";
    private static final Long YOUTUBE_PLAYLIST_MAX_RESULTS = 50L;

    private static final String YOUTUBE_VIDEOS_PART = "snippet,contentDetails,statistics"; // video resource properties that the response will include.
    private static final String YOUTUBE_VIDEOS_FIELDS = "items(id,snippet(title,description,thumbnails/high),contentDetails/duration,statistics)"; // selector specifying which fields to include in a partial response.

    private YouTube mYouTubeDataApi;
    private String mTitle;
    private String mSearchQuery;

    public GetPlaylistAsyncTask(YouTube api, String title, String searchQuery) {
        mYouTubeDataApi = api;
        mTitle = title;
        mSearchQuery = searchQuery;
    }

    @Override
    protected Pair<String, List<Playlist>> doInBackground(String... params) {

        SearchListResponse searchResponse;
        try {
            YouTube.Search.List search = mYouTubeDataApi.search().list("id,snippet");

            search.setKey(ApiKey.YOUTUBE_API_KEY);
            search.setQ(mTitle + " " + mSearchQuery);

            search.setType("video");
            search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
            search.setMaxResults(YOUTUBE_PLAYLIST_MAX_RESULTS);
            searchResponse = search.execute();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        if (searchResponse == null) {
            Log.e(TAG, "Failed to get playlist");
            return null;
        }

        ArrayList videoIds = new ArrayList();

        for (SearchResult item : searchResponse.getItems()) {
            videoIds.add(item.getId().getVideoId());
        }
        VideoListResponse videoListResponse = null;
        try {
            videoListResponse = mYouTubeDataApi.videos()
                    .list(YOUTUBE_VIDEOS_PART)
                    .setFields(YOUTUBE_VIDEOS_FIELDS)
                    .setKey(ApiKey.YOUTUBE_API_KEY)
                    .setId(TextUtils.join(",", videoIds)).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new Pair(searchResponse.getNextPageToken(), videoListResponse.getItems());
    }
}

我不知道我的代码有什么问题,如果你能指出我正确的方向,我将不胜感激。提前谢谢!

1 个答案:

答案 0 :(得分:0)

只需将public更改为protected,因为onPostExecute并非公开设计。

    @Override
    protected void onPostExecute(Pair<String, List<Playlist>> result) {
        handleGetPlaylistResult(mPlaylist, result);
    }