youdonotexist
youdonotexist

Reputation: 911

Using the Google API Java Client to parse publicly shared Calendar RSS feeds

I'm attempting to build an Android application that displays Google calendar entries in a list from a public Calendar RSS feed. The Calendar feed is public, but is not shared to any specific user.

I've done just that on the iOS side by using Google's gdata library (in combination with the public Calendar RSS) to parse and display the public calendar feed.

Unfortunately, gdata is not compatible with Android. Google has the new google-api-java-client libraries, but all the sample code is used in tandem with a google account living on the device that then authenticates through OAuth.

Is there any way to grab and parse public feeds with these new libraries without having to store the authentication information on the device? Or even authenticate at all?

Upvotes: 1

Views: 1076

Answers (1)

youdonotexist
youdonotexist

Reputation: 911

I found the solution. A good example of this can been seen with the current YouTube example on the Google Api Java Client example.

http://code.google.com/p/google-api-java-client/wiki/SampleProgram

The missing part of the puzzle was having to:

  1. Extend the GoogleUrl class (I used CalendarUrl)
  2. Create objects that match the jsonc output of the feed (you can get the jsonc output by appending &alt=jsonc to the end of the feed URL)
  3. Annotate (@Key("NameOfField")) the objects from step 2 to match jsonc names

Once you've done that, the following code will build and get everything that you need:

HttpTransport transport = new NetHttpTransport();
final JsonFactory jsonFactory = new JacksonFactory();
HttpRequestFactory factory = transport.createRequestFactory(new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest request) {
            // set the parser
            JsonCParser parser = new JsonCParser(jsonFactory);
            request.addParser(parser);
            // set up the Google headers
            GoogleHeaders headers = new GoogleHeaders();
            headers.setApplicationName("Google-CalendarSample/1.0");
            headers.gdataVersion = "2";
            request.setHeaders(headers);
        }
});

CalendarUrl url = new CalendarUrl(YOUR_FEED_URL);

HttpRequest request = factory.buildGetRequest(url);
CalendarFeed feed = request.execute().parseAs(CalendarFeed.class);

Upvotes: 1

Related Questions