Exception in parsing google webmasters api response

116 views
Skip to first unread message

Darshan Mehta

unread,
Aug 17, 2015, 2:29:25 PM8/17/15
to google-oauth-java-client
Hello,

I am using google webmasters java client to get the impressions and clicks data over a period of time. However, while executing search analytics query, I get the following exception:

java.lang.IllegalArgumentException: key ctr
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:871)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:374)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:347)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:87)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:459)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
        ...
Caused by: java.lang.IllegalArgumentException: key ctr, field private java.util.List com.google.api.services.webmasters.model.SearchAnalyticsQueryResponse.rows
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:871)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:462)
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:771)
... 8 more
Caused by: java.lang.IllegalArgumentException: key ctr, field private java.util.List com.google.api.services.webmasters.model.SearchAnalyticsQueryResponse.rows
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:871)
at com.google.api.client.json.JsonParser.parseArray(JsonParser.java:638)
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:730)
... 10 more
Caused by: java.lang.IllegalArgumentException: key ctr, field private java.lang.Double com.google.api.services.webmasters.model.ApiDataRow.ctr
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:871)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:462)
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:771)
... 12 more
Caused by: java.lang.IllegalArgumentException: number field formatted as a JSON string must use the @JsonString annotation
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:92)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:49)
at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:836)
... 14 more

Below is the code that I am using to query:

public void testWebmasters(String token){
    try{
         Credential credential = getCredential(token);
         Webmasters service = new Webmasters.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName("WebmastersCommandLine")
            .build();
    com.google.api.services.webmasters.Webmasters.Sites.List list = service.sites().list();
    SitesListResponse execute = list.execute();
    List<WmxSite> siteEntry = execute.getSiteEntry();
    for(WmxSite site : siteEntry){
    System.out.println(site.getSiteUrl());
    System.out.println(site.getPermissionLevel());
    SearchAnalyticsQueryRequest request = new SearchAnalyticsQueryRequest();
    request.setStartDate("2015-08-01");
    request.setEndDate("2015-08-05");
    request.setDimensions(new ArrayList<String>(){{add("date");}});
    SearchAnalyticsQueryResponse response = service.searchanalytics().query(site.getSiteUrl(), request).execute();
    for(ApiDataRow row : response.getRows()){
    System.out.println(row.getClicks());
    System.out.println(row.getImpressions());
    System.out.println(row.getCtr());
    row.getPosition();
    }
    }
    }catch(Exception e){
    e.printStackTrace();
    }
    }

Below line in the code throws exception:
SearchAnalyticsQueryResponse response = service.searchanalytics().query(site.getSiteUrl(), request).execute();

Going through exception stack trace and code of google java library, it seems this can be resolved by adding @JSONString annotation for 'ctr' property of 'ApiDataRow' class. However, this being a third party library, I don't want to change the code. Also, may be it would fail on another field if I change the code for this.

Any suggestions how to resolve this?

Thanks in advance.

Thar Nge Lay(275756583)

unread,
Jul 8, 2016, 3:12:26 PM7/8/16
to google-oauth-java-client
Reply all
Reply to author
Forward
0 new messages