如果用户名和密码已知,我正在尝试访问用户的雅虎日历。我找到了一个使用 CalDav api 来完成任务的代码片段。代码使用 2 个库 sardine.jar 和 ical4j.jar。该代码的文档说该代码适用于谷歌日历和雅虎日历。只有我们必须相应地更改 CalDav 服务器的 url。代码适用于谷歌日历,但是当我更改雅虎日历的代码时,它不起作用。我收到以下错误:
Stacktrace:] 根本原因 net.fortuna.ical4j.data.ParserException:第 1 行错误:预期 [-3],读取 [-1]
我认为这是由于 google 和 yahoo 的 ics 文件格式不匹配,或者没有为 yahoo 创建 ics 文件。当我从浏览器而不是雅虎使用这个 URL 时,我发现了一个谷歌日历的 ics 文件。
这是我使用的代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import net.fortuna.ical4j.data.CalendarBuilder;
import net.fortuna.ical4j.data.ParserException;
import net.fortuna.ical4j.filter.Filter;
import net.fortuna.ical4j.filter.PeriodRule;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Period;
import net.fortuna.ical4j.model.Property;
import com.googlecode.sardine.Sardine;
import com.googlecode.sardine.SardineFactory;
public class sardineMain {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
Sardine sardine = SardineFactory.begin("Email","password");
String url= "https://www.google.com/calendar/dav/emailid@gmail.com/events/";
java.util.Calendar start = java.util.Calendar.getInstance();
java.util.Calendar end = java.util.Calendar.getInstance();
end.add(java.util.Calendar.MONTH, 1);
//Fri May 25 08:56:36 KST 2012==Mon Jun 25 08:56:36 KST 2012
Period period = new Period(new DateTime(start.getTime()),new DateTime(end.getTime()) );
Filter filter = new Filter(new PeriodRule(period));
//Collection<CalanderQueryOutput> results = new ArrayList<CalanderQueryOutput>();
InputStream EventStream = sardine.get(url);
BufferedReader br = new BufferedReader(new InputStreamReader(EventStream));
CalendarBuilder builder = new CalendarBuilder();
Calendar calendar = builder.build(br);
List eventsToday = (List) filter.filter(calendar.getComponents(Component.VEVENT));
CalanderQueryOutput caldavOutput = new CalanderQueryOutput();
for (Iterator i = eventsToday.iterator(); i.hasNext();) {
Component component = (Component) i.next();
String CreatedS = component.getProperty(Property.CREATED).toString().trim();
String SummaryS = component.getProperty(Property.SUMMARY).toString().trim();
String StatusS = component.getProperty(Property.STATUS).toString().trim();
String StartDateS = component.getProperty(Property.DTSTART).toString().trim();
String EndDateS = component.getProperty(Property.DTEND).toString().trim();
/*System.out.println(CreatedS);
System.out.println(SummaryS);
System.out.println(StatusS);
System.out.println(StartDateS);
System.out.println(EndDateS);*/
System.out.println(component);
}
}
这些是网址:
谷歌日历:https ://www.google.com/calendar/dav/ <>@gmail.com/events/
雅虎!日历:https : //caldav.calendar.yahoo.com/dav/ <>@yahoo.es/Calendar//
请让我知道代码中的内容或如何以编程方式获取雅虎日历的 ics 文件。