JAVA版本-得到GOOGLE CALENDAR,显示在你的主页中......
JAVA版本-得到GOOGLE CALENDAR,显示在你的主页中......
第一次看到是在
http://ajax.phpmagazine.net/2006/04/howto_integrate_google_calenda.html
题目为<<Howto integrate Google Calendar in your website using AJAX>>
当时感觉很好啊,这样不用直接每次都访问calendar.google.com站点了.PHP不熟悉,就使用JAVA来获取.当然其步骤没有PHP简单,需要专门写一个方法来获取远程文件,然后获取文件内容,反馈给静态页面.思路大致如此,见下面代码:
eventrss.jsp
附注:GOOGLE CALENDAR地址已经不再可用.为了安全其见!
第一次看到是在
http://ajax.phpmagazine.net/2006/04/howto_integrate_google_calenda.html
题目为<<Howto integrate Google Calendar in your website using AJAX>>
当时感觉很好啊,这样不用直接每次都访问calendar.google.com站点了.PHP不熟悉,就使用JAVA来获取.当然其步骤没有PHP简单,需要专门写一个方法来获取远程文件,然后获取文件内容,反馈给静态页面.思路大致如此,见下面代码:
package com;
import java.io.*;
import java.net.*;
public class GetUrlFile{
/*
public static void main( String [] args ){
new GetUrlFile().getFile( "http://www.google.com/calendar/feeds/yongboy@gmail.com/private-63a1b8b847df62e680c3dd265a566594/basic" );
}
*/
//获取远程文件
public String getFile( String fileUrl ){
StringBuffer sb = new StringBuffer();
try
{
URL url = new URL( fileUrl );
DataInputStream in=new DataInputStream( url.openStream());
int result = -1;
while( ( result = in.read() ) != -1 )
{
sb.append( (char)result );
}
in.close();
}
catch(Exception eurl)
{
System.out.println( eurl.toString() );
sb.append( "get the romote file wrong!" );
}
return sb.toString();
}
}
//获取数据的JSP页面:import java.io.*;
import java.net.*;
public class GetUrlFile{
/*
public static void main( String [] args ){
new GetUrlFile().getFile( "http://www.google.com/calendar/feeds/yongboy@gmail.com/private-63a1b8b847df62e680c3dd265a566594/basic" );
}
*/
//获取远程文件
public String getFile( String fileUrl ){
StringBuffer sb = new StringBuffer();
try
{
URL url = new URL( fileUrl );
DataInputStream in=new DataInputStream( url.openStream());
int result = -1;
while( ( result = in.read() ) != -1 )
{
sb.append( (char)result );
}
in.close();
}
catch(Exception eurl)
{
System.out.println( eurl.toString() );
sb.append( "get the romote file wrong!" );
}
return sb.toString();
}
}
eventrss.jsp
<%@ page contentType="text/xml;charset=UTF-8"%>
<!--到如获取远程文件的类-->
<%@ page import="com.GetUrlFile" %>
<%
//这里要换成你自己的私有地址
String calendarUrl = "http://www.google.com/calendar/feeds/yongboy@gmail.com/private-63a1b8b847df62e680c3dd265a566594/basic";
//得到返回的数据
String getStr = new GetUrlFile().getFile( calendarUrl );
//必须经过编码转换
getStr = new String( getStr.getBytes( "ISO-8859-1" ), "UTF-8" );
//输出到页面
out.write( getStr );
%>
其它没有变化,首页见下:<!--到如获取远程文件的类-->
<%@ page import="com.GetUrlFile" %>
<%
//这里要换成你自己的私有地址
String calendarUrl = "http://www.google.com/calendar/feeds/yongboy@gmail.com/private-63a1b8b847df62e680c3dd265a566594/basic";
//得到返回的数据
String getStr = new GetUrlFile().getFile( calendarUrl );
//必须经过编码转换
getStr = new String( getStr.getBytes( "ISO-8859-1" ), "UTF-8" );
//输出到页面
out.write( getStr );
%>
<html>
<head>
<title>Google Calendar Events - RSS Reader</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<script language="javascript" type="text/javascript">
var RSSRequestObject = false; // XMLHttpRequest Object
var Backend = 'eventrss.jsp'; // Backend url
window.setInterval("update_timer()", 1200000); // update the data every 20 mins
if (window.XMLHttpRequest) // try to create XMLHttpRequest
RSSRequestObject = new XMLHttpRequest();
if (window.ActiveXObject) // if ActiveXObject use the Microsoft.XMLHTTP
RSSRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
/*
* onreadystatechange function
*/
function ReqChange() {
// If data received correctly
if (RSSRequestObject.readyState==4) {
// if data is valid
if (RSSRequestObject.responseText.indexOf('invalid') == -1)
{
// Parsing Feeds
var node = RSSRequestObject.responseXML.documentElement;
// Get the calendar title
var title = node.getElementsByTagName('title').item(0).firstChild.data;
content = '<div class="channeltitle">'+title+'</div>';
// Browse events
var items = node.getElementsByTagName('entry');
if (items.length == 0) {
content += '<ul><li><div class=error>No events</div></li></ul>';
} else {
content += '<ul>';
for (var n=items.length-1; n >= 0; n--)
{
var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
var Summary = items[n].getElementsByTagName('summary').item(0).firstChild.data;
var itemLink = items[n].getElementsByTagName('id').item(0).firstChild.data;
try
{
var itemPubDate = '<font color=gray>['+items[n].getElementsByTagName('published').item(0).firstChild.data+'] ';
}
catch (e)
{
var itemPubDate = '';
}
content += '<li>'+itemPubDate+'</font><a href="'+itemLink+'">'+itemTitle+'</a></li>';
}
content += '</ul>';
}
// Display the result
document.getElementById("ajaxreader").innerHTML = content;
// Tell the reader the everything is done
document.getElementById("status").innerHTML = "Done.";
}
else {
// Tell the reader that there was error requesting data
document.getElementById("status").innerHTML = "<div class=error>Error requesting data.<div>";
}
HideShow('status');
}
}
/*
* Main AJAX RSS reader request
*/
function RSSRequest() {
// change the status to requesting data
HideShow('status');
document.getElementById("status").innerHTML = "Requesting data ";
// Prepare the request
RSSRequestObject.open("GET", Backend , true);
// Set the onreadystatechange function
RSSRequestObject.onreadystatechange = ReqChange;
// Send
RSSRequestObject.send(null);
}
/*
* Timer
*/
function update_timer() {
RSSRequest();
}
function HideShow(id){
var el = GetObject(id);
if(el.style.display=="none")
el.style.display='';
else
el.style.display='none';
}
function GetObject(id){
var el = document.getElementById(id);
return(el);
}
</script>
<body onload="RSSRequest();">
<h2>Google Calendar Events - RSS Reader</h2>
<div id="status" style="display:none"></div>
<div id="ajaxreader"></div>
<div class="about">
<p><strong>About :</strong> This script is reading from a shared Calendar on Google Calendars
just showing how to display your events from your calendar on your websites.
<br/><br/>For details read post howto <a href=http://ajax.phpmagazine.net/2006/04/howto_integrate_google_calenda.html>integrate Google Calendar in your website using AJAX</a></p>
<p>This demo is insipired from my <a href="http://ajax.phpmagazine.net/2005/11/ajax_rss_reader_step_by_step_t.html">AJAX RSS reader</a></p>
<p>Author : Hatem [hatem @ php.net]</p>
<p>Copyright 2006 (C) <a href="http://ajax.phpmagazine.net/">AJAX Magazine</a></p>
</div>
</body>
</html>
<head>
<title>Google Calendar Events - RSS Reader</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<script language="javascript" type="text/javascript">
var RSSRequestObject = false; // XMLHttpRequest Object
var Backend = 'eventrss.jsp'; // Backend url
window.setInterval("update_timer()", 1200000); // update the data every 20 mins
if (window.XMLHttpRequest) // try to create XMLHttpRequest
RSSRequestObject = new XMLHttpRequest();
if (window.ActiveXObject) // if ActiveXObject use the Microsoft.XMLHTTP
RSSRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
/*
* onreadystatechange function
*/
function ReqChange() {
// If data received correctly
if (RSSRequestObject.readyState==4) {
// if data is valid
if (RSSRequestObject.responseText.indexOf('invalid') == -1)
{
// Parsing Feeds
var node = RSSRequestObject.responseXML.documentElement;
// Get the calendar title
var title = node.getElementsByTagName('title').item(0).firstChild.data;
content = '<div class="channeltitle">'+title+'</div>';
// Browse events
var items = node.getElementsByTagName('entry');
if (items.length == 0) {
content += '<ul><li><div class=error>No events</div></li></ul>';
} else {
content += '<ul>';
for (var n=items.length-1; n >= 0; n--)
{
var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
var Summary = items[n].getElementsByTagName('summary').item(0).firstChild.data;
var itemLink = items[n].getElementsByTagName('id').item(0).firstChild.data;
try
{
var itemPubDate = '<font color=gray>['+items[n].getElementsByTagName('published').item(0).firstChild.data+'] ';
}
catch (e)
{
var itemPubDate = '';
}
content += '<li>'+itemPubDate+'</font><a href="'+itemLink+'">'+itemTitle+'</a></li>';
}
content += '</ul>';
}
// Display the result
document.getElementById("ajaxreader").innerHTML = content;
// Tell the reader the everything is done
document.getElementById("status").innerHTML = "Done.";
}
else {
// Tell the reader that there was error requesting data
document.getElementById("status").innerHTML = "<div class=error>Error requesting data.<div>";
}
HideShow('status');
}
}
/*
* Main AJAX RSS reader request
*/
function RSSRequest() {
// change the status to requesting data
HideShow('status');
document.getElementById("status").innerHTML = "Requesting data ";
// Prepare the request
RSSRequestObject.open("GET", Backend , true);
// Set the onreadystatechange function
RSSRequestObject.onreadystatechange = ReqChange;
// Send
RSSRequestObject.send(null);
}
/*
* Timer
*/
function update_timer() {
RSSRequest();
}
function HideShow(id){
var el = GetObject(id);
if(el.style.display=="none")
el.style.display='';
else
el.style.display='none';
}
function GetObject(id){
var el = document.getElementById(id);
return(el);
}
</script>
<body onload="RSSRequest();">
<h2>Google Calendar Events - RSS Reader</h2>
<div id="status" style="display:none"></div>
<div id="ajaxreader"></div>
<div class="about">
<p><strong>About :</strong> This script is reading from a shared Calendar on Google Calendars
just showing how to display your events from your calendar on your websites.
<br/><br/>For details read post howto <a href=http://ajax.phpmagazine.net/2006/04/howto_integrate_google_calenda.html>integrate Google Calendar in your website using AJAX</a></p>
<p>This demo is insipired from my <a href="http://ajax.phpmagazine.net/2005/11/ajax_rss_reader_step_by_step_t.html">AJAX RSS reader</a></p>
<p>Author : Hatem [hatem @ php.net]</p>
<p>Copyright 2006 (C) <a href="http://ajax.phpmagazine.net/">AJAX Magazine</a></p>
</div>
</body>
</html>
附注:GOOGLE CALENDAR地址已经不再可用.为了安全其见!