Triển khai: Phân trang
Ví dụ sau đây cho biết cách truy xuất các tập hợp kết quả bổ sung cho các truy vấn API dữ liệu YouTube (phiên bản 3).
API sử dụng tham số maxResults
để cho biết số lượng mục cần đưa vào phản hồi API. Hầu hết các phương thức list
của API (videos.list
, playlists.list
, v.v.) đều hỗ trợ tham số đó.
Nếu có kết quả bổ sung cho một truy vấn, thì phản hồi API sẽ chứa thuộc tính nextPageToken
, thuộc tính prevPageToken
hoặc cả hai. Sau đó, bạn có thể sử dụng các giá trị của các thuộc tính đó để đặt tham số pageToken
nhằm truy xuất thêm một trang kết quả.
Ví dụ: cụm từ tìm kiếm sau đây sẽ truy xuất kết quả tìm kiếm cho 10 video được xem nhiều nhất khớp với cụm từ tìm kiếm "chó trượt ván":
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?
part=snippet
&maxResults=10
&order=viewCount
&q=skateboarding+dog
&type=video
Phản hồi API chứa 10 kết quả trùng khớp đầu tiên cho truy vấn cũng như một thuộc tính nextPageToken
có thể dùng để truy xuất 10 kết quả tiếp theo:
Truy vấn bên dưới truy xuất 10 kết quả tiếp theo cho truy vấn:
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?
part=snippet
&maxResults=10
&order=viewCount
&pageToken=CAoQAA
&q=skateboarding+dog
&type=video
Lưu ý: Bạn có thể cần cập nhật giá trị của tham số pageToken
để hoàn tất yêu cầu này trong Trình khám phá API. Thực thi truy vấn truy xuất 10 kết quả đầu tiên để lấy giá trị tham số pageToken
chính xác.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2024-11-23 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2024-11-23 UTC."],[[["The YouTube Data API (v3) uses the `maxResults` parameter to determine the number of items returned in a query response."],["API `list` methods, such as `videos.list` and `playlists.list`, support the `maxResults` parameter for pagination."],["If more results are available, the API response includes `nextPageToken` and/or `prevPageToken` properties."],["These token values can be used to set the `pageToken` parameter to retrieve additional result pages."],["The initial request fetches the first page of results, and subsequent requests use the `pageToken` from the previous response to get the next page of results."]]],["The YouTube Data API (v3) uses `maxResults` to specify the number of items in a response. `list` methods support this, and responses with additional results include `nextPageToken` or `prevPageToken`. These tokens, used with the `pageToken` parameter, retrieve further result sets. For instance, a query can get 10 most viewed videos, and a `nextPageToken` in the response allows fetching the next 10 by adding this value in the `pageToken` parameter.\n"]]