将Long转换为Integer

时间:2017-04-08 13:14:06

标签: java android youtube

我在Github上使用YoutubePlaylist-Master库作为我的Android应用程序。 我想只有频道的最后一个视频,我试图修改代码,但我有一个问题:

当我将值设置为1时,我发生错误,因为我使用的是Interger而不是Long。

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

    //see: https://developers.google.com/youtube/v3/docs/playlistItems/list
    private static final String YOUTUBE_PLAYLIST_PART = "snippet";
    private static final String YOUTUBE_PLAYLIST_FIELDS = "pageInfo,nextPageToken,items(id,snippet(resourceId/videoId))";
    //see: https://developers.google.com/youtube/v3/docs/videos/list
    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.

        (...)

    @Override
    protected Pair<String, List<Video>> doInBackground(String... params) {
        final String playlistId = params[0];
        final String nextPageToken;

        if (params.length == 2) {
            nextPageToken = params[1];
        } else {
            nextPageToken = null;
        }

        PlaylistItemListResponse playlistItemListResponse;
        try {
            playlistItemListResponse = mYouTubeDataApi.playlistItems()
                    .list(YOUTUBE_PLAYLIST_PART)
                    .setPlaylistId(playlistId)
                    .setPageToken(nextPageToken)
                    .setFields(YOUTUBE_PLAYLIST_FIELDS)
                    .setMaxResults(YOUTUBE_PLAYLIST_MAX_RESULTS)
                    .setKey(ApiKey.YOUTUBE_API_KEY)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

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

        List<String> videoIds = new ArrayList();

        // pull out the video id's from the playlist page
        for (PlaylistItem item : playlistItemListResponse.getItems()) {
            videoIds.add(item.getSnippet().getResourceId().getVideoId());
        }

        // get details of the videos on this playlist page
        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(playlistItemListResponse.getNextPageToken(), videoListResponse.getItems());
    }
}

所以问题是:

我有2个VAR:

private static final Long YOUTUBE_PLAYLIST_MAX_RESULTS = 1L;

private static final Integer YOUTUBE_LAST_V = 1;

protected Pair<String, List<Video>> doInBackground(String... params)课程中,当我尝试将.setMaxResults(YOUTUBE_PLAYLIST_MAX_RESULTS)更改为.setMaxResults(YOUTUBE_LAST_V)时,我遇到错误:Error:(70, 36) error: incompatible types: Integer cannot be converted to Long

2 个答案:

答案 0 :(得分:0)

setMaxResults()不接受int类型数据,因此请将YOUTUBE_PLAYLIST_MAX_RESULTS类型更改为long(后缀L),如下所示:

private static final Long YOUTUBE_LAST_V = 1L;

答案 1 :(得分:0)

只需设置

即可
private static final Integer YOUTUBE_PLAYLIST_MAX_RESULTS = 1;

这是一个静态的最终字段, - 所以它永远不会改变,Integer就足够了......