我希望代码能够识别重复或重复订阅事件中的错误,
在YouTube API引入之初,我从代码段中摘录了上述代码段,并对其进行了修改,即订阅插入:
private List<String> getDataFromApi() throws IOException {
String channelId = "UCEgdi0XIXXZ-qJOFPf4JSKw";
// Create a resourceId that identifies the channel ID.
ResourceId resourceId = new ResourceId();
resourceId.setChannelId(channelId);
resourceId.setKind("youtube#channel");
// Create a snippet that contains the resourceId.
SubscriptionSnippet snippet = new SubscriptionSnippet();
snippet.setResourceId(resourceId);
// Create a request to add the subscription and send the request.
// The request identifies subscription metadata to insert as well
// as information that the API server should return in its response.
Subscription subscription = new Subscription();
subscription.setSnippet(snippet);
YouTube.Subscriptions.Insert subscriptionInsert =
mService.subscriptions().insert("snippet,contentDetails", subscription);
try {
Subscription returnedSubscription = subscriptionInsert.execute();
if (returnedSubscription != null){
System.out.println("\n================== Returned Subscription ==================\n");
System.out.println(" - Id: " + returnedSubscription.getId());
System.out.println(" - Title: " + returnedSubscription.getSnippet().getTitle());
etNama1.setText(returnedSubscription.getId());
etNama2.setText(returnedSubscription.getSnippet().getTitle());
}
} catch (GoogleJsonResponseException e) {
etNama1.setText(e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
e.printStackTrace();
} catch (IOException e) {
etNama1.setText(e.getMessage());
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
} catch (Throwable t) {
etNama1.setText(t.getMessage());
System.err.println("Throwable: " + t.getMessage());
t.printStackTrace();
}
// Print information from the API response.
return null;
}