如何在我的 android 应用程序中使用 woocommerce rest api 进行自定义分页?

How to do custom pagination using woocommerce rest api in my android app?

在我的 android 应用程序中,我使用 woocommerce rest api 从我的库存中获取产品。根据文档 "Requests that return multiple items will be paginated to 10 items by default." 我需要为每个请求获取 25 个项目。根据文档,我尝试了很多东西,但不幸的是我没有取得任何成功。

分页我试过了

"mysite/wc-api/v3/products/"+"?page="+pageNum;

使用它我获得了 pageNum = 1 的前 10 个产品, 11-20 即使用 pageNum = 2 的下一组 10 个项目,依此类推。我需要每页获取 25 个项目。

我尝试使用

"mysite/wc-api/v3/products?filter[limit]="+25;

但是这个 returns 我的前 25 项。现在,我如何获得下一组 25 个项目,即 26 到 50 个项目。

我不会用

"mysite/wc-api/v3/products?filter[limit]="+50;

因为它会给我前 50 个项目。

我指的是https://woocommerce.github.io/woocommerce-rest-api-docs/

我只需要端点。

WooCommerce 根本不支持这个。

唯一的方法是调用第 1 到第 3 页,只显示第 1-2 页和第 3 页的第 5 个产品

最后我自己想出了解决办法:

如果我们需要为每个请求获取 25 个项目。

端点类似于:

"mysite/wc-api/v3/products?filter[limit]=25&filter[offset]="+offset;

这将一次性提供 25 个项目,在每个请求中我需要发送偏移量,即在每个后续请求中为 0,25,50,75.....。

试试这个

https://www.[website_name].com/wp-json/wc/v3/products?[consumer_key_here]&[consumer_secret_key_here]&per_page=15&page=1

[website_name] = Your Website Name Here
[consumer_key_here] = Your Consumer Key Here
[consumer_secret_key_here] = Your Consumer Secret Key Here

您可以轻松创建分页

https://www.website.com/wp-json/wc/v3/products?consumer_key=ckxxxxxxxxxx&consumer_secret=xxxxxxxxxx&page=15&per_page=1

API 在 HTTP 请求中传递的参数
per_page = 15
页 = 1

您必须为 page 创建一个变量(用于页码)然后您将 page 数值每个增加 1你提出请求的时间。

https://www.website.com/wp-json/wc/v3/products?consumer_key=ckxxxxxxxxxx&consumer_secret=xxxxxxxxxx&page=15&per_page=2

如果我们需要为每个请求获取 15 个项目。

这将在第一个 HTTP 请求中获得前 15 个产品,在第二个请求中获得另外 15 个产品,依此类推。