如果用户名和密码已知,我正在尝试访问用户的雅虎日历。我找到了一个代码片段,它使用CalDav api来完成任务。代码使用2个库sardine.jar和ical4j.jar。该代码的文档说该代码适用于谷歌日历和雅虎日历。只有我们必须相应地将URL更改为CalDav服务器。代码适用于谷歌日历,但当我更改雅虎日历的代码时,它无法正常工作。 我收到以下错误:
Stacktrace:]有根本原因 net.fortuna.ical4j.data.ParserException:第1行出错:预期[-3],读取[-1]
我认为这是由于谷歌和雅虎的ics文件格式不匹配或没有创建雅虎的ics文件。当我从浏览器使用此网址但未使用雅虎时,我找到了谷歌日历的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);
}
}
这些是网址:
Google日历:https://www.google.com/calendar/dav/&lt;&gt; @ gmail.com / events /
雅虎!日历:https://caldav.calendar.yahoo.com/dav/&lt;&gt; @ yahoo.es / Calendar //
请让我知道代码是什么,或者我如何以编程方式获取雅虎日历的ics文件。