如何在 Google Apps Reseller API 中使用 nextPageToken?
How to use nextPageToken in Google Apps Reseller API?
我正在编写一个简单的 Java 应用程序,它使用经销商 API 从 Google 应用程序经销商处获取所有客户订阅。但是,由于每页最大大小为 100,我无法对结果进行分页,而且我无法找出下一页标记。这是到目前为止的代码。如何使用 setNextPageToken() 并打印所有结果?
public static void main(String[] args) throws IOException {
GoogleCredential credentials = GoogleApiUtil.getCredentials();
Reseller service = new Reseller.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials)
.setApplicationName("ResellerDemo").build();
boolean allResultsRead = false;
String nextPageToken = null;
while (!allResultsRead) {
Subscriptions result = service.subscriptions().list()
.execute();
List<Subscription> subscriptions = result.getSubscriptions();
nextPageToken = result.getNextPageToken();
if (subscriptions == null || subscriptions.size() == 0) {
System.out.println("No subscriptions found.");
} else {
System.out.println("Subscriptions:");
for (Subscription sub : subscriptions) {
System.out.printf("%s (%s, %s)\n",
sub.getCustomerId(),
sub.getSkuId(),
sub.getPlan().getCommitmentInterval());
}
}
if (result.getNextPageToken() == null) {
allResultsRead = true;
} else {
result.setNextPageToken(result.getNextPageToken());
}
}
这应该有效。当您实现真实的东西时,请确保您已使用指数退避重试。
public void retrieveAll() throws IOException, InterruptedException {
GoogleCredential credentials = GoogleApiUtil.getCredentials();
Reseller service = new Reseller.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials)
.setApplicationName("ResellerDemo").build();
String nextPageToken = null;
do {
Subscriptions subsList = service.subscriptions().list().setMaxResults(Long.valueOf(100)).setPageToken(nextPageToken)
.execute();
printSubscirptions(subsList);
nextPageToken = subsList.getNextPageToken();
} while(nextPageToken != null && !"".equals(nextPageToken));
}
private void printSubscirptions(Subscriptions subscriptions){
System.out.println("Subscriptions:");
for (Subscription sub : subscriptions.getSubscriptions()) {
System.out.printf("%s (%s, %s)\n",
sub.getCustomerId(),
sub.getSkuId(),
sub.getPlan().getCommitmentInterval());
}
}
我正在编写一个简单的 Java 应用程序,它使用经销商 API 从 Google 应用程序经销商处获取所有客户订阅。但是,由于每页最大大小为 100,我无法对结果进行分页,而且我无法找出下一页标记。这是到目前为止的代码。如何使用 setNextPageToken() 并打印所有结果?
public static void main(String[] args) throws IOException {
GoogleCredential credentials = GoogleApiUtil.getCredentials();
Reseller service = new Reseller.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials)
.setApplicationName("ResellerDemo").build();
boolean allResultsRead = false;
String nextPageToken = null;
while (!allResultsRead) {
Subscriptions result = service.subscriptions().list()
.execute();
List<Subscription> subscriptions = result.getSubscriptions();
nextPageToken = result.getNextPageToken();
if (subscriptions == null || subscriptions.size() == 0) {
System.out.println("No subscriptions found.");
} else {
System.out.println("Subscriptions:");
for (Subscription sub : subscriptions) {
System.out.printf("%s (%s, %s)\n",
sub.getCustomerId(),
sub.getSkuId(),
sub.getPlan().getCommitmentInterval());
}
}
if (result.getNextPageToken() == null) {
allResultsRead = true;
} else {
result.setNextPageToken(result.getNextPageToken());
}
}
这应该有效。当您实现真实的东西时,请确保您已使用指数退避重试。
public void retrieveAll() throws IOException, InterruptedException {
GoogleCredential credentials = GoogleApiUtil.getCredentials();
Reseller service = new Reseller.Builder(HTTP_TRANSPORT, JSON_FACTORY, credentials)
.setApplicationName("ResellerDemo").build();
String nextPageToken = null;
do {
Subscriptions subsList = service.subscriptions().list().setMaxResults(Long.valueOf(100)).setPageToken(nextPageToken)
.execute();
printSubscirptions(subsList);
nextPageToken = subsList.getNextPageToken();
} while(nextPageToken != null && !"".equals(nextPageToken));
}
private void printSubscirptions(Subscriptions subscriptions){
System.out.println("Subscriptions:");
for (Subscription sub : subscriptions.getSubscriptions()) {
System.out.printf("%s (%s, %s)\n",
sub.getCustomerId(),
sub.getSkuId(),
sub.getPlan().getCommitmentInterval());
}
}