April
28th,
2012
這個禮拜初,最後總算弄好標記閱讀(Mark Read)後,Google Reader App總算也完成了。不過由於接下來可能會把一些時間拿去研究iOS。最後決定把這部份的研究做一個總整理。這一篇文章會包括以下的技術: Google API 在Windows Phone 7.1上面的相關使用 對於Google Reader API的JSON的相關使用 一些簡單的Windows Phone原始碼(source code)的討論 關於RSS Reader App: 首先我想在這裡先稍微整理一下,一個簡單的RSS Reader的Windows Phone APP是相當簡單的。其實只要會簡單的Webclient加上去使用內建的資料格式就可以完成,這裡微軟也提供完整的原始碼與介紹。本篇介紹的部分主要都是專注在Google Reader的部分。 Google Reader API: 由於我是在Windows Phone上面做開發(Windows Phone 7.1)所以無法直接使用Google Data上面的相關API,在這裡也要特別解釋,如果你只是WPF或是一般Windows程式~ 你可以直接使用Google Data而不需要一步步的完成Google Reader API的部分,在這裡把之前文章列過的Google Reader API整理一次: Unofficial Google Reader API R2 google reader on Google code pyfeed on Google code 一個簡單Google Reader APP的流程: 接下來我會用碼個Google Reader 會使用到的流程來介紹該如何去撰寫,首先一個Google Reader APP會有以細的流程: 登入,也就是登入你的Google Reader 帳號 取得登入者的相關資訊 瀏覽全部尚未閱讀的列表 打開某個標籤(Label)內去看裡面還沒閱讀的文章 打開該文章網站並且將其標記已經閱讀(mark as read) 接下來我會針對以下的部分,開始整理一些原始碼: 1. 登入,也就是登入你的Google Reader 帳號 在這裡會使用到基本的Google 登入部分,你可以直接在你的瀏覽器上頭打入以下的鏈結。 > > [https://www.google.com/reader/api/0/token?ck=212121212&client=scroll](https://www.google.com/reader/api/0/token?ck=212121212&client=scroll) > > 稍微解釋一下參數: > > ck: 就是時間標記(timestamp),你可以用DateTime.Now.Ticks.ToString()拿到 > > > > client:先填入scroll的方式 > > 如果你已經登入過的話,一樣會有一樣的結果 public void LoginGoogleReader() { string email = "YOUR_EMAIL"; string passwd = "YOUR_PASSWORD"; string auth_params = string.Format("https://www.google.com/accounts/ClientLogin?accountType=HOSTED_OR_GOOGLE&Email=" + email + "&Passwd=" + passwd + "&service=reader&source=J-MyReader-1.0"); HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(auth_params); <font color="#ff0000"> httpRequest.Method = "POST";</font> RequestState myRequestState = new RequestState(); myRequestState.request = httpRequest; IAsyncResult result = (IAsyncResult)httpRequest.BeginGetResponse(new AsyncCallback(AuthRespCallback), myRequestState); } 但是你也需要去把登入回來的SID與AuthID去parse出來,以下提供相關方法: private void ParseLogin(string responseString) { string[] arrs =...
繼續閱讀
April
25th,
2012
上一篇文章有講解利用HttpWebRequest的方式來完成Google Reader中的標記閱讀,但是因為edit-tag本身就不屬於太消耗時間的動作。與其去用HttpWebRequest的非同步的方式,不如使用容易又好用的WebClient,所以我就把我原來程式碼做了一點修改。
public void MarkArticleAsRead()
{
CurrentTransType = Transaction_Type.MARK_AS_READ;
string auth_params = string.Format("https://www.google.com/reader/api/0/edit-tag?client=scroll&format=joson&ck=" + DateTime.Now.Ticks.ToString());
string postData = "";
postData += "&i=tag:google.com,2005:reader/item/fa42c976c848ecf4";
postData += "&a=user/-/state/com.google/read";
postData += "&s=feed/http://funiphone.pixnet.net/blog/feed/rss";
// Note the token must get within 30 mins
postData += "&T=//mUESPUMtDyZh6BaFXd-CqQ";
WebClient wc = new WebClient();
wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
wc.Headers["Authorization"] = "GoogleLogin auth=" + UserAuth;
wc.Headers["Cookie"] = "SID=" + UserSid;
try
{
wc.UploadStringAsync(new Uri(auth_params), "POST", postData);
}
catch (WebException e)
{
//handle error if any
}
}
<font face="Georgia">幾個需要注意的地方如下:</font>
UserAuth與UserSid 需要先對Google API做login的部分
Content-type是必須要先填成這樣,我試過uff8會失敗~
繼續閱讀
April
23rd,
2012
雖然網路上沒有完整使用C#與Windows Phone 7.1的範例,不過還是盡量用中文來寫這篇吧。其實網路上英文的資料應該也不少,不論是pyrfeed這裡的Google API列表,還是Unofficial Google Reader API或是R2 google reader on Google code。不過要完整的C#還有Windows Phone 7.1的sample code我也是想辦法K了半天這三份API文件然後不斷的測試,在這裡就列出一些片段的程式碼。 public void MarkArticleAsRead(string newID) { string auth_params = string.Format("https://www.google.com/reader/api/0/edit-tag?client=scroll&format=joson&ck=" + DateTime.Now.Ticks.ToString()); HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(auth_params); httpRequest.Method = "POST"; httpRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; httpRequest.Headers["Authorization"] = "GoogleLogin auth=" + UserAuth; httpRequest.Headers["Cookie"] = "SID=" + UserSid; httpRequest.BeginGetRequestStream(new AsyncCallback(GetPostRequestStreamCallback), httpRequest); } 不過這裡有一些需要注意的是SID與auth ID需要另外區擷取,這在我其他文章提到。不過接下來就是重點的部分。也就是post data該如何填、該放哪些資料。 private static void GetPostRequestStreamCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; // End the operation Stream postStream = request.EndGetRequestStream(asynchronousResult); string postData = ""; postData += "&i=tag:google.com,2005:reader/item/fa42c976c848ecf4"; postData += "&a=user/-/state/com.google/read"; postData += "&s=feed/http://funiphone.pixnet.net/blog/feed/rss"; // Note the token must get within 30 mins postData += "&T=//mUESPUMtDyZh6BaFXd-CqQ" + LoginToken; // Convert the string into a byte array. byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Write to the request stream. postStream.Write(byteArray, 0, postData.Length); postStream.Close(); // Start the asynchronous operation to get the response request.BeginGetResponse(new AsyncCallback(GetPostResponseCallback), request); } 在這裡也要注意的是Token需要再30分鐘之內取得~ 建議是呼叫之前馬上去取得token來使用。還有這裡的參數”a=”user 之後有user id 需要填,在這裡就不列出來。 &i=要填的是news 的id,還有&s=要填的是rss ID,你可以在另外一個參數”http://www.google.com/reader/api/0/stream/contents/user” 去取得。最後是結果的部分: private static void GetPostResponseCallback(IAsyncResult asynchronousResult) { HttpWebRequest...
繼續閱讀
April
23rd,
2012
本篇文章應該會用中文撰寫到完,接續著我前幾篇的文章,我一共寫了幾篇跟Google Reader溝通有關的文章。也讓我來仔細敘述當初為何會有這些文章的出現吧。
大概是兩個月前XDite大大在plurk po了一篇 “我要在一個月之內把Reeder幹出來” (大概是這樣吧!雖然我忘記原文了,而且大大好像後來也刪除了)。其實那一篇我看了以後很震撼~ 對喔!! 可以利用完全仿製另外一個商用APP的方式來練功。
於是我也開始撰寫APP,由於我自己只有iPhone,但是卻沒有任何MAC的電腦可以給我提供開發的環境,也只好拿公司最容易找到工具。Microsoft的 Visual Studio來撰寫Windows Phone。
一路上從四月才開始把專案建起來到現在,從完全不會寫Windows Phone到搞定Data Binding、知道如何使用Lambda與Delegate、也慢慢把Windows Phone原件搞懂,當然好久沒摸的WPF也開始拿回來K了一陣,到最後的LINQ、JSON(這個方便應該之後會寫一篇文章來做個說明)。其實寫好一個Google Reader App能夠學習的APP技巧真的不少,在這裡也稍微列一下:
為了登入Google API的服務, 一開始要學習的就是如何去處理網路溝通的方式,面對的就是 HttpWebRequest與WebClient。 當然這篇文章也幫助我很多。在這時候一併就把Lanbda與Delegate 的觀念學起來,畢竟你得要處理threading與UI呈現的問題。
登入Google Reader API之後就沒事情了嗎? 還要好好的去處理溝通過來得資料,這裡你當然可以乖乖的使用XML的方式或是直接跳到JSON的方式。 還好之前學習的Linq與JSON就派上用場。不過Google Reader的JSON資料也怪怪的~ 為了去處理這些東西~也讓我對JSON的觀念更佳的清楚了。
抓回來的Label列表與文章列表要如何處理呢?這時候又要跟WPF裡面的ListBox開始奮戰,這時候你又得跟Data Binding還有WPF XAML的語法來奮鬥。
別忘了最後~ 由於你需要離線閱讀還以記錄使用者的登入資訊。這時候又要跟Local database on Windows Phobne來奮鬥。不過這個算簡單的,畢竟LinQ步算是太難入門的東西。
到目前為止利用自己上班剩餘時間,大概忙了三個禮拜的結果,目前已經可以登入與看文章。由於在今天好不容易把Google Reader裡面的設定閱讀(mark it as read)也搞定了。 現在也才可以寫篇文章做點記錄。不過接下來才是Reeder令人激賞的好用地方,我也先把自己的TODO 列一下:
順暢與讀體驗與文章下載(裡面包含著完全不影響使用者閱讀的離線下載)
將你喜歡的文章轉載到各個社群媒體Facebook,Evernote…. 等 (希望懂了JSON可以讓之後的路更順暢)
使用者介面得更加美化 (這是我的盲點~~~~~)
好啦!! 今天也總算把基本功能都完成了! 繼續努力了~~~
繼續閱讀
April
19th,
2012
SyntaxHighlighter是一個在網站上相當好用的東西。我終於也在www.evanlin.comblog把他安裝好了</br>不過呢~ 由於Window Live Writer 還沒有找到真的讓我覺得很好用的部分</br>這個Plugin 不好用(更正: 因為不支援最新版本的Syntaxhighlighter 3.0.8.3)</br>以下是我加入到MT 模板的code(不過這是用VSpaste)先貼的 <span style="color:green;"><!-- Include required JS files --> </span><span style="color:blue;"><</span><span style="color:#a31515;">script </span><span style="color:red;">type</span><span style="color:blue;">="text/javascript" </span><span style="color:red;">src</span><span style="color:blue;">="js/shCore.js"></</span><span style="color:#a31515;">script</span><span style="color:blue;">> <</span><span style="color:#a31515;">script </span><span style="color:red;">type</span><span style="color:blue;">="text/javascript" </span><span style="color:red;">src</span><span style="color:blue;">="js/shBrushJScript.js"></</span><span style="color:#a31515;">script</span><span style="color:blue;">> <</span><span style="color:#a31515;">script </span><span style="color:red;">type</span><span style="color:blue;">="text/javascript" </span><span style="color:red;">src</span><span style="color:blue;">="js/shBrushCSharp.js"></</span><span style="color:#a31515;">script</span><span style="color:blue;">> <</span><span style="color:#a31515;">script </span><span style="color:red;">type</span><span style="color:blue;">="text/javascript" </span><span style="color:red;">src</span><span style="color:blue;">="js/shBrushCss.js"></</span><span style="color:#a31515;">script</span><span style="color:blue;">> <</span><span style="color:#a31515;">script </span><span style="color:red;">type</span><span style="color:blue;">="text/javascript" </span><span style="color:red;">src</span><span style="color:blue;">="js/shBrushJava.js"></</span><span style="color:#a31515;">script</span><span style="color:blue;">> <</span><span style="color:#a31515;">script </span><span style="color:red;">type</span><span style="color:blue;">="text/javascript" </span><span style="color:red;">src</span><span style="color:blue;">="js/shBrushXml.js"></</span><span style="color:#a31515;">script</span><span style="color:blue;">> <</span><span style="color:#a31515;">link </span><span style="color:red;">href</span><span style="color:blue;">="css/shCore.css" </span><span style="color:red;">rel</span><span style="color:blue;">="stylesheet" </span><span style="color:red;">type</span><span style="color:blue;">="text/css" /> <</span><span style="color:#a31515;">link </span><span style="color:red;">href</span><span style="color:blue;">="css/shThemeDefault.css" </span><span style="color:red;">rel</span><span style="color:blue;">="stylesheet" </span><span style="color:red;">type</span><span style="color:blue;">="text/css" /> </span><span style="color:green;"><!-- Include required JS files --> </span><span style="color:blue;"><</span><span style="color:#a31515;">script </span><span style="color:red;">type</span><span style="color:blue;">="text/javascript"> </span><span style="color:#a31515;">SyntaxHighlighter.all() </span><span style="color:blue;"></</span><span style="color:#a31515;">script</span><span style="color:blue;">> </span> 話說~ VSpaste 這個plugin 也很好用~ 跨Blog又不用安裝~ 不過可能支援語系會少一點 以下是使用這個Windows Live Writer Plugin(Windows Live Writer Source Code plugin for SyntaxHighlighter) , 而且也支援最新版本的syntaxhighlighter 3.0.8.3 <!-- Include required JS files --> <script type="text/javascript" src="js/shCore.js"></script>...
繼續閱讀
April
11th,
2012
Google data is good API tool for C# or other language to connect with Google related service, however here is no official Google Reader API on document. and Google data don’t support for Windows phone (only for Windows mobile for now). However it could be found on web for some of unofficial document for Google Reader API as follows: Unofficial Google Reader API R2 google reader on Google code pyfeed on Google code But however to come out detail implement for Windows Phone, it quit need time to work detail. Allow me try to list some major problems of this. Most important of using HttpWebRequest is the error repose > > The remote server returned an error: NotFound > > It is most common error code your will find during you try to login or get any feedback on Window Phone. It could be either you missing your GET parameter...
繼續閱讀