【问题标题】:Set Google Calendar query parameters with google-java-api-client in Android在 Android 中使用 google-java-api-client 设置 Google 日历查询参数
【发布时间】:2011-06-12 00:25:05
【问题描述】:

我正在构建一个 URL 以使用 google-javi-api 访问用户的 Google 日历:

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

返回这个网址:

"https://www.google.com/calendar/feeds/user@gmail.com/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”替换,和号被替换为“&”

返回的不正确的url是:

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

我很确定我的结果集为空的原因是因为这些字符替换。如何在原始 URL 中附加新参数?

**如果你想知道我是如何构建这个网址的,我正在使用来自这个sample Android implementation of Google CalendarCalendarURL 类。

编辑

更具体地说,在 CalendarURL 类中,我可以将部分添加到 URL 的“路径”,但我找不到包含查询参数的方法。这个 API 不包含指定参数的方法吗?

【问题讨论】:

    标签: java android google-api-java-client


    【解决方案1】:

    使用 google-java-client-api 创建 URL 的正确方法是扩展 GoogleUrl 对象。 (我在这里使用谷歌纵横作为示例。我创建了一个 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
    

    【讨论】:

    • 感谢您的回复。我在上面的答案中发布的用法是获取带参数的 URL 的不正确方法吗?你的方式看起来是完成这项工作的好方法,我毫不怀疑它有效,但你的方式和我的方式有什么区别?我问的唯一原因是因为我在看到差异时学得最好。
    • 查看我对您的回答的评论 :) 使用 @Key 注释,您有一种通用的方式来声明您的 URL(包括参数)。相同的 @Key 属性可用于从响应中检索数据。它使您的代码更易于阅读,因为它是强类型的。google-api-java-client 的工作方式是使用 @Key 注释。
    • 此外,google-api-java-client 将发布大量生成的客户端库,包括这些 Url 对象。因此,作为开发人员,您开始使用 Url 对象(您不必再自己编写它们),通过查看它们的字段,您会立即了解接口的方式(= API = url + 参数)看起来像是在与 API 交互。
    • 很抱歉再次这样做......但是......另一个问题......stackoverflow.com/questions/6403293/… - ;-)......
    【解决方案2】:

    经过一番认真的挖掘,我发现了如何使用 google-java-api 包含查询参数。

    要将这些Query Parameters 中的任何一个添加到 URL,请执行以下操作:

    构建基本 CalendarUrl 后,调用 .put("Key", "Value") 添加查询参数。例如:

    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");
    

    我刚刚在 Google 的项目主页中偶然发现了一个隐藏在一堆未经过滤的“问题”中的线程。有很多关于使用 gData api 的文档,但没有关于 google-java-api 的文档。我花了将近 2 天的时间才找到这个简单的方法调用。非常令人沮丧。我希望读到这篇文章的人没有通过我所经历的来了解如何完成这个简单但至关重要的任务。应该有更好的记录。

    【讨论】:

    • google-api-java-client 中的 GoogleUrl 对象实际上是作为 Maps 实现的,解释了为什么可以使用 put(key,value) 添加参数。但是,这是相当低级的东西,作为开发人员,您不应该将其视为地图。通过使用 @Key 属性注释某些字段(表示参数),您可以完成同样的事情。这样代码就更清晰了(请参阅我的答案示例)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    相关资源
    最近更新 更多