XReadr–A simple Google Reader App on Windows Phone 7

Finally, I decide to release the source code of my Windows Phone Google Reader App. Please feel free to https://xreadr.codeplex.com/ here. Here is my project summary: Project Description XReadr is a simple Windows Phone 7 app using Google Reader API. It already has login, browse and mark it as read function here. XReadr is simple Windows Phone 7 App which using Google Reader API. It also has follow function: Login page which using HttpWebRequest Browse Unread Labels Browse every news on Label I also put the first draft of source code, I hope it could help all of you which want to write more Windows News App here. More implementation detail about how Windows Phone to use Google Reader API, please refer my blog here: http://www.evanlin.com/blog/archives/001138.html http://www.evanlin.com/blog/archives/001139.html It write by Traditional Chinese, feel free to ask me via English.
繼續閱讀

[C#][WINDOWS PHONE] 關於頁面處理的相關小細節(改變初始頁面與頁面間傳遞參數)

這幾天開始把原來的RSS Reader APP程式多加頁面後,發現了一些小問題。網路上或許還算是好找。 但是我稍微整理一下,其實Pou’s blog兩篇文章有相當清楚的解釋 Windows Phone 7 - 動態轉換初始的Default Page Windows Phone 7 – Navigation Framework原理概論 在此只把常遇到的兩個問題整理一下: 如何改變初始頁面(How to change default page on Windows Phone) 修改文件”WMAppManifest.xml”以下程式碼: <Tasks> <DefaultTask Name="_default" NavigationPage="MainPage.xaml" /> </Tasks> 如何在頁面中傳遞參數(How to pass parameter over pages on Windows Phone) 在頁面間傳遞參數可以把它當成是網頁的GET模式(就是?Param_1=value_1&Param_2=value_2) 在原來的頁面可以用以下的指令傳遞 string mylogin = "/Page2.xaml"; mylogin += "?Param1=" + "VALUE1"; mylogin += "&Param2=" + "VALUE2"; if (!String.IsNullOrWhiteSpace(mylogin)) { this.NavigationService.Navigate(new Uri(mylogin, UriKind.Relative)); } 再接收的Page2就必須要有以下的方式去接受參數 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { //Get parameter string myParam1 = NavigationContext.QueryString["Param1"]; string myParam2 = NavigationContext.QueryString["Param2"]; } 這樣就可以了~~~ 參考網頁: http://www.eugenedotnet.com/2011/03/passing-values-to-windows-phone-7-pages-uri-paremeters-or-querystring/ http://stackoverflow.com/questions/3892271/how-do-i-change-the-startup-page-on-a-wp7-silverlight-app http://www.dotblogs.com.tw/pou/archive/2011/06/05/27148.aspx http://www.dotblogs.com.tw/pou/archive/2011/04/30/23929.aspx http://www.dotblogs.com.tw/pou/archive/2011/01/23/20967.aspx
繼續閱讀

[C#][Windows Phone][JSON]如何用C#完成一個簡單的Google Reader APP(下)

3. 瀏覽全部尚未閱讀的列表 接上篇文章,接下來會開始講解如何去獲得使用者所有標籤(Label)的文章。 一開始首先一樣是先提供這裡會用到的相關Google Reader API的講解 > > [https://www.google.com/reader/api/0/unread-count?allcomments=true&output=json&ck=12121&client=scroll](https://www.google.com/reader/api/0/unread-count?allcomments=true&output=json&ck=12121&client=scroll) > > 你就可以在瀏覽器上面獲得你要的資訊。你如果有登入你可能會獲得像是以下的一些資料。 接下來是取得資料的相關原始碼: public void GetGoogleReaderUnReadCount() { string auth_params = string.Format("https://www.google.com/reader/api/0/unread-count?allcomments=true&output=json&ck=" + DateTime.Now.Ticks.ToString() + "&client=scroll"); HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(auth_params); httpRequest.Method = "GET"; httpRequest.Headers["Authorization"] = "GoogleLogin auth=" + UserAuth; httpRequest.Headers["Cookie"] = "SID=" + UserSid; httpRequest.BeginGetResponse(new AsyncCallback(ResponseCallbackInner), httpRequest); } 抓回資料後,就開始要去parse這些資料,這邊會用到大量的JSON相關技巧,我也盡量試著解釋清楚一點給大家知道。首先,會拿到以下的資料,以下是以我的資訊當作範例~跟你拿到的可能會有點差異。 { "max":1000, "unreadcounts":[ { "id":"user/-/label/教學網站", "count":9, "newestItemTimestampUsec":"1335595557150290" }, { "id":"user/-/label/Funny", "count":83, "newestItemTimestampUsec":"1335621983685977" }, { "id":"user/-/label/專業評論", "count":38, "newestItemTimestampUsec":"1335617137314302" } } 這些資料就是JSON的資料,關於JSON是甚麼與JSON.NET該怎麼使用的部分,網路上有許多相關的程式碼,我這裡就不詳述了。 根據上面資料排序的結果,我們需要去抓unreadcounts下面的子結點,而他的資料格式如下: public class ServerUnreadResult //JSON { public string id { get; set; } public string count { get; set; } public string newestItemTimestampUsec { get; set; } } 所以在我們程式碼中,就是可以依照以下的安排: private void ParseUnreadList(string responseString) { //JSON Parse JObject googleSearch = JObject.Parse(responseString); // get JSON result objects into a list IList<JToken> results = googleSearch["unreadcounts"].Children().ToList(); // serialize JSON results into .NET objects IList<ServerUnreadResult> searchResults = new List<ServerUnreadResult>(); foreach (JToken result in results) { ServerUnreadResult searchResult = JsonConvert.DeserializeObject<ServerUnreadResult>(result.ToString()); //Parse the label name // ex: user/06771113693638414260/label/Win8 string...
繼續閱讀

[C#][Windows Phone][JSON]如何用C#完成一個簡單的Google Reader APp(上)

這個禮拜初,最後總算弄好標記閱讀(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 =...
繼續閱讀

[C#][Windows Phone]利用WebCLient去完成Google Reader標記閱讀(Mark as read)

上一篇文章有講解利用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會失敗~
繼續閱讀

[C#][Windows Phone]如何使用Google reader API標記已經閱讀 (mark as read)

雖然網路上沒有完整使用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...
繼續閱讀