【问题标题】:Google Calendar API android create event谷歌日历 API android 创建事件
【发布时间】:2017-03-28 10:20:44
【问题描述】:

我正在尝试使用 Google Calendar API 在 android App 中创建一个带有文件 a (.pdf) 的事件:Create Events

public static void addAttachment(Calendar calendarService, Drive driveService, String calendarId,
    String eventId, String fileId) throws IOException {
  File file = driveService, android .files().get(fileId).execute();
  Event event = calendarService.events().get(calendarId, eventId).execute();

  List<EventAttachment> attachments = event.getAttachments();
  if (attachments == null) {
    attachments = new ArrayList<EventAttachment>();
  }
  attachments.add(new EventAttachment()
      .setFileUrl(file.getAlternateLink())
      .setMimeType(file.getMimeType())
      .setTitle(file.getTitle()));

  Event changes = new Event()
      .setAttachments(attachments);
  calendarService.events().patch(calendarId, eventId, changes)
      .setSupportsAttachments(true)
      .execute();
}

我完全复制了这个但它不起作用,Android Studio 放入红色的 getAlternateLink() 并且 getTitle() 没有重新调整,特别是以下行:

attachments.add(new EventAttachment()
      .setFileUrl(file.getAlternateLink())
      .setMimeType(file.getMimeType())
      .setTitle(file.getTitle()));

【问题讨论】:

  • 究竟是什么不起作用?有什么例外吗?
  • Android Studio 放入红色的 getAlternateLink() 和 getTitle()
  • 从您复制代码的文档中,here 声明“在将附件支持添加到您现有的在本地存储事件的应用程序。如果您不先执行同步,您可能会无意中从用户的事件中删除现有附件。另外,请注意,您正在执行的代码演示了如何更新现有事件以添加附件。

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


【解决方案1】:

在 Drive:v3 不退出 getALternateLink() 在应用程序中将 de 版本更改为 v2

//    compile('com.google.apis:google-api-services-drive:v3-rev64-1.22.0') {
//        exclude group: 'org.apache.httpcomponents'
//    }

放这个

compile('com.google.apis:google-api-services-drive:v2-rev123-1.18.0-rc'){
    exclude group: 'org.apache.httpcomponents'
}

【讨论】:

    【解决方案2】:

    对于 Drive API V3,您需要获取驱动文件的元数据。首先获取 Drive 文件,然后获取 Meta data 。元数据对象具有所有 3 个值。

    Task<Metadata> metadataTask = getDriveResourceClient().getMetadata(driveFile.getDriveId().asDriveResource());
    
    Tasks.await(metadataTask);
    Metadata meta =  metadataTask.getResult();
    
    Event event = mService.events().get(calendarId, eventId).execute();
    List<EventAttachment> attachments = event.getAttachments();
    if (attachments == null) {
        attachments = new ArrayList<>();
    }
    
    Event changes = new Event();
    attachments.add(new EventAttachment()
            .setFileUrl(meta.getAlternateLink())
            .setMimeType(meta.getMimeType())
            .setTitle(meta.getTitle()));
    changes.setAttachments(attachments);
    
    mService.events()
            .patch(calendarId, eventId, changes)
            .setSupportsAttachments(true)
            .execute();
    

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2012-10-10
      • 1970-01-01
      • 2015-01-14
      • 2018-09-13
      相关资源
      最近更新 更多