Reputation: 1025
Im trying to retrieve the location from my campaign using adwords api, im having an error :
Exception in thread "main" java.lang.ClassCastException: com.google.api.ads.adwords.axis.v201506.cm.Language cannot be cast to com.google.api.ads.adwords.axis.v201506.cm.Location at adwords.axis.v201506.basicoperations.GetCampaigns.locationExample(GetCampaigns.java:312) at adwords.axis.v201506.basicoperations.GetCampaigns.main(GetCampaigns.java:86)
And this is my code im using Java :
public static void locationExample(
AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId) throws Exception {
CampaignCriterionServiceInterface campaignCriterionService =
adWordsServices.get(session, CampaignCriterionServiceInterface.class);
int offset = 0;
boolean morePages = true;
// Create selector.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder
.fields(
CampaignCriterionField.Id,
CampaignCriterionField.CriteriaType,
CampaignCriterionField.LocationName)
.orderAscBy(CampaignCriterionField.Id)
.offset(offset)
.limit(PAGE_SIZE)
.build();
while (morePages) {
CampaignCriterionPage page = campaignCriterionService.get(selector);
if (page.getEntries() != null && page.getEntries().length > 0) {
for (CampaignCriterion campaignCriterionResult : page.getEntries()) {
Location location = (Location) campaignCriterionResult.getCriterion();
System.out.println("Location ID: "+location.getId());
System.out.println("Location Name: "+location.getLocationName());
System.out.println("Location Bid adjustment: ");
System.out.println("Location Type: "+location.getType());
// System.out.println("Location Reach: "+location.get);
}
} else {
System.out.println("No ad group criteria were found.");
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
morePages = offset < page.getTotalNumEntries();
}
}
My main question is how can i retrieve the location and language from adwords api.
Upvotes: 0
Views: 1089
Reputation: 401
The CampaignCriterion retrieves all criterions associated to the campaign, so you can not cast the criterion object to one type (Location) because this may be another class. This line is wrong:
Location location = (Location) campaignCriterionResult.getCriterion();
Instead, you should check the type before cast the object. For example:
for(CampaignCriterion criterion: criterions) {
if(criterion.getCriterion().getCriterionType().equals("Location")) {
Location location = (Location) criterion.getCriterion();
// Your code
}
}
Upvotes: 1