Created
October 2, 2012 07:54
-
-
Save ryugoo/3817169 to your computer and use it in GitHub Desktop.
HTTP Client (Event fire pattern)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function () { | |
| Ti.App.addEventListener("app:getContentFire", function (listenEvt) { | |
| var data = JSON.parse(listenEvt.data); | |
| alert("データの取得に成功しました!"); | |
| alert(data); | |
| }); | |
| var wrapper = Ti.UI.createWindow(); | |
| var base = Ti.UI.createWindow({ | |
| title: "Base", | |
| backgroundColor: "#FFFFFF" | |
| }); | |
| var button = Ti.UI.createButton({ | |
| title: "Get Content" | |
| }); | |
| button.addEventListener("click", function (clickEvt) { | |
| // HTTP Client | |
| var xhr = Ti.Network.createHTTPClient(); | |
| // Onload handler | |
| xhr.onload = function () { | |
| // 今回は Twitter のタイムライン情報 (JSON) を取ってくるので、 responseText を使う | |
| Ti.App.fireEvent("app:getContentFire", { | |
| data: xhr.responseText | |
| }); | |
| }; | |
| // Error handler | |
| xhr.error = function (xhrErr) { | |
| alert("データの取得時にエラーが発生しました"); | |
| }; | |
| // XHR open method | |
| xhr.open("GET", "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=TitaniumMobileJ"); | |
| // XHR send method | |
| xhr.send(null); | |
| }); | |
| base.add(button); | |
| var navGroup = Ti.UI.iPhone.createNavigationGroup({ | |
| window: base | |
| }); | |
| wrapper.add(navGroup); | |
| wrapper.open(); | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment