in Traditional Chinese <div style="width:640px" id="__ss_4728414"><strong style="display:block;margin:12px 0 4px"><a href="http:// www.slideshare.net/kewang/20100607-how-to-access-the-database-using-the-android" title="How to access the external database using the android">How to access the external database using the android</a></strong><object id="__sse4728414" width="640" height="535"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf? doc=20100607howtoaccessthedatabaseusingtheandroid-100710233815- phpapp02&rel=0&stripped_title=20100607-how-to-access-the-database- using-the-android" /><param name="allowFullScreen" value="true"/ ><param name="allowScriptAccess" value="always"/><embed name="__sse4728414" src="http://static.slidesharecdn.com/swf/ ssplayer2.swf? doc=20100607howtoaccessthedatabaseusingtheandroid-100710233815- phpapp02&rel=0&stripped_title=20100607-how-to-access-the-database- using-the-android" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="535"></embed></object><div style="padding:5px 0 12px">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/kewang">Mu Chun Wang</a>.</div></div>
這幾天一直在研究要怎麼將Android與外部的資料庫存取,因為書中都只有講到如何與Android本身的<a href="http:// www.sqlite.org/">SQLite</a>做存取,可是對於外部資料庫存取的部分卻是少的可憐,拜了一下Google大神也是沒有什麼結論,於是我就自己想了一套還蠻蠢的方法,不過有顧慮到與資料庫存取的安全性啦。 一般我們在網頁存取資料庫的時候,當然都是連到<a href="http://en.wikipedia.org/wiki/ DMZ_(computing)">DMZ</a>裡面的web server,然後web server再用各種方式(ADO.NET, ODBC, JDBC......等)與防火牆內的資料庫做存取,可是這僅限於Web應用程式可以如此存取,如果你寫的是手機程式(iPhone APP、Android APP)的話,那這種方法理論是可行,可是實際上是不可行的。 如果想要讓手機程式(這邊以Android為例)讀取外部資料庫時,最簡單的方法就是用JDBC去連外部資料庫,可是這會發生一個安全上的問題。也就是 資料庫必須要從防火牆內拉出來放到DMZ,要不然JDBC是永遠都沒辦法存取外部資料庫的,所以就有一堆人想到了,對web server用web service來做存取資料庫的動作,這就解決了安全性的問題,可是在開發時卻還是極為不便。 如果大家有用過Rails開發程式的話,應該對於它那個強大的<a href="http://zh.wikipedia.org/zh-tw/ REST">RESTful機制</a>和<a href="http://zh.wikipedia.org/zh-tw/%E5%AF %B9%E8%B1%A1%E5%85%B3%E7%B3%BB%E6%98%A0%E5%B0%84">ORM</a>愛不釋手吧,用ORM來解決了 許多操作資料庫的問題,也從最根本的方式用RESTful來存取資料庫。所以我就架了一台可以跑Rails的web server在DMZ,而且因為我 只要用到RESTful和ORM而已,所以我將web server從apache改為<a href="http:// www.lighttpd.net/">lighttpd</a>,其實還有其他更輕量的web server,只不過我只會這兩套而已 XD。 再來就是設定Rails的RESTful以及建立DB migration,這些都完成了之後,最後把RESTful的那四個method以及七個 action實作出來,這樣子我只要在Android上面寫HTTP method,並且對這一台web server做存取,我就可以完成對外部資料 庫的存取了,而且也不用擔心安全性的問題,真是太強大了啦!!! 只不過在寫Android的HTTP method時,發現了一些以前從沒遇到的問題,那就是<a href="http:// en.wikipedia.org/wiki/List_of_HTTP_status_codes">HTTP status code</a>居然 出現了417,這個問題真的是困擾了我很久,後來找到了這篇文章「<a href="http://overhrd.com/? p=189">Remove Expect header in Android’s org.apache.http</a>」,總算是 解決了這個鳥問題。 我覺得這個問題真的是還蠻難找出來的,動用了<a href="http://www.wireshark.org/">Wireshark</ a>, Google Chrome的Developer Tools, <a href="http:// www.tcpdump.org/">tcpdump</a>,還有一堆Rails的log才知道怎麼解決,所以想說寫這篇文章跟大家分享一下實作的架構以及甘苦談 XDDDD,總之大家看了簡報應該就知道怎麼解決囉。 參考資料: <a href="http://www.sqlite.org/">SQLite Home Page</a> <a href="http://en.wikipedia.org/wiki/DMZ_(computing)">DMZ (computing) - Wikipedia, the free encyclopedia</a> <a href="http://zh.wikipedia.org/zh-tw/REST">REST - 維基百科,自由的百科全書</a> <a href="http://zh.wikipedia.org/zh-tw/%E5%AF %B9%E8%B1%A1%E5%85%B3%E7%B3%BB%E6%98%A0%E5%B0%84">物件關聯對映 - 維基百科,自由的百科全書 </a> <a href="http://www.lighttpd.net/">lighttpd fly light</a> <a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes">List of HTTP status codes - Wikipedia, the free encyclopedia</a> <a href="http://www.wireshark.org/">Wireshark · Go deep.</a> <a href="http://www.tcpdump.org/">TCPDUMP/LIBPCAP public repository</ a> <a href="http://www.plurk.com/p/5lpx8m">kewang 說 用rails的ORM跟RESTful來做為 DB存取的web service,然後再用Android來存取這個web service (dance) - #5lpx8m</a> <a href="http://www.plurk.com/p/5mtbr3">kewang 說 HTTP Status Code 417讓我 找了兩天解決的方法,原來是HTTP header的問題,要移除Expect: 100-continue呀!!! http://overhrd.com/?p= - #5mtbr3</a> -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

