在Android中使用google-java-api-client设置Google日历查询参数

问题描述

|| 我正在构建一个URL,以使用google-javi-api来访问用户Google日历:
CalendarUrl url = CalendarUrl.forEventFeed(\"accountName\",\"private\",\"full\");
返回我这个网址:
\"https://www.google.com/calendar/Feeds/[email protected]/private/full?prettyprint=true\"
我想使用startMin和startMax参数设置此URL的参数,以便该URL最终看起来像这样:
\"https://www.google.com/calendar/Feeds/default/private/full?start-min=2011-06-00T00:00:00&start-max=2011-06-24T23:59:59\"
我所有的尝试都失败了,在记录了返回的URL后,我发现\“?\”被\“%3F \”替换,而&符被\“&\”替换 返回的错误网址是:
\"https://www.google.com/calendar/Feeds/default/private/full%3Fstart-min=2011-06-00T00:00:00&start-max=2011-06-24T23:59:59\"
我很确定我的结果集为null的原因是由于这些字符替换。如何在原始网址后附加新参数? **如果您想知道如何构建此url,请使用Google日历的示例Android实现中的CalendarURL类。 编辑 更具体地说,在CalendarURL类中,我可以将部分添加到URL的“路径”中,但是找不到包含查询参数的方法。此API是否不包含指定参数的方法?     

解决方法

        使用google-java-client-api创建URL的正确方法是扩展GoogleUrl对象。 (我在这里使用Google Latitude作为示例。我创建了一个GoogleUrl对象,稍后您将了解如何使用它)。 Google URL对象 您构造一个扩展GoogleUrl的URL对象 您可以使用@Key注释来注释要在URL上自定义的参数。 您提供了一个采用根URL的构造函数。 使用pathParts.add方法将零件添加到上下文中 一个示例URL对象如下所示:
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;
  }
}
用法 您可以使用此对象来构造URL,只需填写参数(带@Key注释的字段),然后执行build()方法即可获取该字符串的字符串表示形式:
    LatitudeUrl latitudeUrl = LatitudeUrl.forLocation();
    latitudeUrl.maxResults=\"20\";
    latitudeUrl.minTime=\"123\";
    latitudeUrl.minTime=\"456\";

    System.out.println(latitudeUrl.build());
输出:
https://www.googleapis.com/latitude/v1/location?max-results=20&min-time=456
    ,        经过一番认真的研究,我发现了如何使用google-java-api来包含查询参数。 要将任何这些查询参数添加到URL,请执行以下操作: 构建基本CalendarUrl之后,调用.put(\“ Key \”,\“ Value \”)以添加查询参数。例如:
CalendarUrl eventFeedUrl = CalendarUrl.forEventFeed(\"[email protected]\",\"private\",\"full\");

  eventFeedUrl.put(\"start-min\",\"2011-06-01T00:00:00\");
  eventFeedUrl.put(\"start-max\",\"2011-06-22T00:00:00\");
我碰巧偶然发现了一个埋在Google项目首页中未过滤的“问题”垃圾中的线程。有很多使用gData api的文档,但是google-java-api没有任何内容。我花了将近2天的时间找到了这个简单的方法调用。非常沮丧。我希望无论谁读了这篇文章都不会经历我所经历的事情,以发现如何完成这个简单而又至关重要的任务。应该更好地记录下来。