English 中文(简体)
How to get a single GoogleCalendar CalendarEventEntry by its ID?
原标题:

I m trying to retrieve a single CalendarEventEntry from a Google Calendar by the event ID but I can t find the way to do it. It seems that the API doesn t provide a method for this, though it suggests to make a query to the feed calendar using Query. The drawback of this is that the ID is not one of the parameters considered.

I think that a possible way to achieve this would be to get the CalendarEventFeed associated with our calendar and then iterate over the resulting list of events as follows:

CalendarService service = new CalendarService(applicationName);
service.setUserCredentials(userName,password);
CalendarEventFeed myFeed = service.getFeed(feedUrl, CalendarEventFeed.class);
List <CalendarEventEntry> entries =  myFeed.getEntries();

for (CalendarEventEntry e : entries){
    if (e.getId().equals(id)){
       return e;
    }
    }

Do you know any easier and more straight solution to achieve this ??

Thanks in advance!

问题回答

You can get the URL of the event by calling calendarEventEntry.getEditLink().getHref(). It is actually the url of the calendar plus the event id.

Take a look at the Data API Developer Guide for code samples.

The guys question has basis, there was no code to answer this question. Find below the code i used to answer this question. I modified your code slightly

import com.google.gdata.client.*;
import com.google.gdata.client.calendar.*;
import com.google.gdata.data.*;
import com.google.gdata.data.acl.*;
import com.google.gdata.data.calendar.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;

import java.net.*;
import java.io.*;
import java.util.List;


public class GetGoogleCalendarEventByID {

public static void main(String[] args) {

    System.out.println("Before Creating CalendarService.");
    CalendarService myService = new CalendarService("exampleCo-exampleApp-1.0");
    System.out.println("After Creating CalendarService.");


    try
    {        
        System.out.println("Going to setUserCredentials.");
        myService.setUserCredentials("email@gmail.com", "password");        

        URL feedUrl =
          new URL("https://www.google.com/calendar/feeds/email@gmail.com/private/full");




        CalendarEventFeed myFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
        List <CalendarEventEntry> entries =  myFeed.getEntries();

        for (CalendarEventEntry e : entries){
            if (e.getId().equalsIgnoreCase("http://www.google.com/calendar/feeds/email%40gmail.com/events/EventIDLastPart")){
               System.out.println("Got the CalendarEventEntry: " + e.getId());
               System.out.println("CalendarEventEntry has text: " + e.getPlainTextContent());
            }
            }              


        System.out.println("Finished getting Event.");        




    }
    catch(Exception e)
    {
      System.out.println("Error : " + e.toString());

    }




}
}

NOTE: feedUrl - That is the Calendar URL you find under the calendar settings

An event id has the format http://www.google.com/calendar/feeds/email%40gmail.com/events/EventIDLastPart

e.g.

http://www.google.com/calendar/feeds/email%40gmail.com/events/ghytrueueyryrgfuur

Hope it helps

Ngonidan





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签