开发者

Set Google Calendar query parameters with google-java-api-client in Android

I'm building a URL to gain access to a users Google calendar with the google-javi-api as such:

CalendarUrl url = CalendarUrl.forEventFeed("accountName", "private", "full");

which returns me this url:

"https://www.google.com/calendar/feeds/user@gmail.com/private/full?prettyprint=true"

I would like to set parameters to this URL with startMin and startMax parameters so the URL would eventually look like this:

"https://www.google.com/calendar/feeds/default/开发者_JAVA技巧private/full?start-min=2011-06-00T00:00:00&start-max=2011-06-24T23:59:59"

All of my attempts at this have failed, and after logging the URL that is being returned, I find that the "?" is being replaced by "%3F" and ampersands are being replaced by "&"

The incorrect url that is bring returned is:

"https://www.google.com/calendar/feeds/default/private/full%3Fstart-min=2011-06-00T00:00:00&start-max=2011-06-24T23:59:59"

I'm pretty sure the reason my result set is null is because of those character replacements. How do I append the original URL with the new parameters?

**If you're wondering how I'm building this url, I'm using the CalendarURL class from this sample Android implementation of Google Calendar.

EDIT

More specifically, in the CalendarURL class, I can add parts to the "path" of the URL, but I can't find a way to include a query parameter. Does this API not include a way to specify a parameter?


The proper way to create a URL using google-java-client-api is to extend the GoogleUrl object. (I'm using Google Latitude here as a sample. I create a GoogleUrl object, and later on you'll see how it gets used).

The Google URL object

  • You construct a URL object extending GoogleUrl
  • You annotate the parameters you would like to customize on the URL using the @Key annotation
  • You provide a constructor that takes on the root url.
  • You add parts to the context using the pathParts.add method

A sample URL object looks like this:

public final class LatitudeUrl extends GoogleUrl {

  @Key
  public String granularity;

  @Key("min-time")
  public String minTime;

  @Key("max-time")
  public String maxTime;

  @Key("max-results")
  public String maxResults;

  /** Constructs a new Latitude URL from the given encoded URI. */
  public LatitudeUrl(String encodedUrl) {
    super(encodedUrl);
  }

  private static LatitudeUrl root() {
    return new LatitudeUrl("https://www.googleapis.com/latitude/v1");
  }

  public static LatitudeUrl forCurrentLocation() {
    LatitudeUrl result = root();
    result.pathParts.add("currentLocation");
    return result;
  }

  public static LatitudeUrl forLocation() {
    LatitudeUrl result = root();
    result.pathParts.add("location");
    return result;
  }

  public static LatitudeUrl forLocation(Long timestampMs) {
    LatitudeUrl result = forLocation();
    result.pathParts.add(timestampMs.toString());
    return result;
  }
}

Usage

You use this object to construct the URL, just fill in your parameters (the @Key annotated fields), and execute the build() method to get a string representation of it :

    LatitudeUrl latitudeUrl = LatitudeUrl.forLocation();
    latitudeUrl.maxResults="20";
    latitudeUrl.minTime="123";
    latitudeUrl.minTime="456";

    System.out.println(latitudeUrl.build());

Output :

https://www.googleapis.com/latitude/v1/location?max-results=20&min-time=456


After some serious digging I found out how to include query parameters using the google-java-api.

To add any of these Query Parameters to a URL, do the following:

After building the basic CalendarUrl, call .put("Key", "Value") to add query parameters. For example:

CalendarUrl eventFeedUrl = CalendarUrl.forEventFeed("user@gmail.com", "private", "full");

  eventFeedUrl.put("start-min", "2011-06-01T00:00:00");
  eventFeedUrl.put("start-max", "2011-06-22T00:00:00");

I just happened to stumble across a thread buried in the midst of a junk-load of unfiltered "issues" at the project home at Google. There is plenty of documentation for using the gData api, but there is NOTHING for the google-java-api. It took me almost 2 days to find this simple method call. Very frustrating. I hope whoever reads this didn't go through what I went through to find out how to accomplish this simple, yet crucial task. It should be better documented.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜