Google API

From no name for this wiki
Revision as of 00:30, 3 September 2008 by 89.217.52.56 (talk) (Google Calendar)
Jump to: navigation, search

Hier ein paar Samples fuer die Google API.

Java

Google Calendar

Sample mit Zugriff auf den Google Kalender. Die Termine werden alle aufgelistet. Click, um Java-Webstart zu starten. Der wesentliche Code findest du hier: GoogleCalendar.java.

Hier der Code:

package googlecalendarjava;

import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class GoogleCalendar {

   public List<String> getEvents(String username, String password) {

       List<String> result = new ArrayList<String>();

       try {
           StringBuffer url = new StringBuffer();
           url.append("http://www.google.com/calendar/feeds/");
           url.append(username);
           url.append("/private/full");

           URL feedUrl = new URL(url.toString());
           CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
           myService.setUserCredentials(username,password);

           CalendarEventFeed queryResult = myService.getFeed(feedUrl, CalendarEventFeed.class);
           List<CalendarEventEntry> entries = queryResult.getEntries();
          
           for (CalendarEventEntry entry : entries) {
               StringBuffer entryString = new StringBuffer();

               //Titel 
               String title = entry.getTitle().getPlainText();
               entryString.append("Title: " + title);

               //Von und bis
               entryString.append(" Start: " + entry.getTimes().get(0).getStartTime());
               entryString.append(" End: " + entry.getTimes().get(0).getEndTime());

               entryString.append("\n");

               result.add(entryString.toString());
           }

       } catch (Exception e) {
           result.add("Error; " + e.getMessage());
       }
       return result;
   }
}

.Net

Google Calendar

Hier eine Klasse um die Events in einem Kalender aufzulisten:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;

namespace GoogleCalendarSample
{
   class GoogleCalendar
   {
       public List<String> GetEvents(string username, string password)
       {
           List<String> result = new List<string>();
           CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
           myService.setUserCredentials(username, password);

           EventQuery query = new EventQuery();
           StringBuilder sb = new StringBuilder();
           sb.Append("http://www.google.com/calendar/feeds/");
           sb.Append(username);
           sb.Append("/private/full");
           query.Uri = new Uri(sb.ToString());
           EventFeed queryResult = myService.Query(query);
           foreach (EventEntry entry in queryResult.Entries)
           {
               StringBuilder stringBuilder = new StringBuilder();
               stringBuilder.Append("Title: " + entry.Title.Text);
               stringBuilder.Append(" Start: " + entry.Times[0].StartTime);
               stringBuilder.Append(" End: " + entry.Times[0].EndTime);
               stringBuilder.Append("\n");
               String stringToAdd = stringBuilder.ToString();
               result.Add(stringToAdd);
           }            
           return result;
        }
   }
}