簡體   English   中英

如何使用Google Calendar API for Java刪除Google日歷上的活動?

[英]How can I use the Google Calendar API for Java to delete an event on my Google Calendar?

我已經搜索了高低,找到了一個例子,因為我正在嘗試的似乎並不起作用。 我收到此錯誤:

com.google.gdata.util.InvalidEntryException: Bad Request
Invalid request URI

    at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:594)
    at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:563)
    at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:552)
    at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:530)
    at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535)
    at com.google.gdata.client.Service.update(Service.java:1563)
    at com.google.gdata.client.Service.update(Service.java:1530)
    at com.google.gdata.client.GoogleService.update(GoogleService.java:583)
    at CalendarConnect.deleteEvent(CalendarConnect.java:37)
    at ProgMain.main(ProgMain.java:26)

這是我正在使用的代碼的示例:

CalendarService service = new CalendarService("Fourth Year Project");
             service.setUserCredentials(username, password);

             URL postUrl = new URL("https://www.google.com/calendar/feeds/default/private/full");
             CalendarEventEntry myEntry = new CalendarEventEntry();

             myEntry.setTitle(new PlainTextConstruct("TEST"));
             myEntry.setContent(new PlainTextConstruct("TEST"));

             DateTime startTime = DateTime.parseDateTime(StartDateTime);
             DateTime endTime = DateTime.parseDateTime(EndDateTime);
             When eventTimes = new When();
             eventTimes.setStartTime(startTime);
             eventTimes.setEndTime(endTime);
             myEntry.addTime(eventTimes);

             CalendarEventEntry insertedEntry = service.update(postUrl, myEntry);

             URL deleteUrl = new URL(insertedEntry.getEditLink().getHref());
             service.delete(deleteUrl);

它被切碎和改變太多了,我不知道我現在在哪里。 有誰遇到過這個問題? 如果是這樣,你是如何克服它的? 有沒有人得到一些代碼的例子,因為谷歌只在他們的解釋中提供一行代碼。

你需要做的唯一事情是在你的活動上有一個“處理”,有不同的方式來獲得它,如果你想要刪除日歷中的特定事件,我會告訴你我該怎么做

    String title = "Event title that i want to delete";

    try{
        URL calendarUrl= new URL(calendar.getLink(Link.Rel.ALTERNATE, Link.Type.ATOM).getHref());
        CalendarEventFeed resultFeed = service.getFeed(calendarUrl, CalendarEventFeed.class);

        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            CalendarEventEntry entry = resultFeed.getEntries().get(i);
            if(entry.getTitle().getPlainText().equals(title)){
                entry.delete();
                break;
            }
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    } 
    catch (ServiceException e) {
        e.printStackTrace();
    }

因此,您只需要設置特定日歷的標題(或任何設置),如果您不知道如何在之前已創建的日歷上獲取“句柄”,我可以向您解釋如何操作。 在我的代碼中,“calendar”是一個ClendarEntry。

你能試一下嗎

CalendarEventEntry insertedEntry = service.insert(postUrl, myEntry);

代替

CalendarEventEntry insertedEntry = service.update(postUrl, myEntry);

我認為您擁有的其余代碼是用於插入事件條目,而您無法使用它調用更新。 如果你把它改成插入,它將插入並刪除條目(如果它工作),我沒有看到它的一個點。 如果您嘗試檢索條目並將其刪除,則帖子中會有一些示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM