Created
April 19, 2011 20:56
-
-
Save pec1985/929634 to your computer and use it in GitHub Desktop.
Get an external JSON file, write it the file system, and convert it to an MD5 hash
This file contains 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
var win = Ti.UI.createWindow(); | |
// here the reponse will be written | |
var texArea = Ti.UI.createTextArea({value:'',top:20,right:20,left:20,bottom:20,backgroundColor:'white', font:{fontSize:20}}); | |
win.add(texArea); | |
win.open(); | |
var xhr = Ti.Network.createHTTPClient(); | |
xhr.onload = function(){ | |
try{ | |
var json = JSON.parse(this.responseText); | |
// show the json in the textArea | |
texArea.value = json; | |
// write it to the file system | |
var file1 = Titanium.Filesystem.getFile( | |
Titanium.Filesystem.applicationDataDirectory, 'json.json' | |
); | |
file1.write(json); | |
// show it as MD5 (after 2 seconds, just for fun) | |
setTimeout(function(){ | |
var file2 = Titanium.Filesystem.getFile( | |
Titanium.Filesystem.applicationDataDirectory, 'json.json' | |
); | |
texArea.value = Ti.Utils.md5HexDigest(file2.toBlob()); | |
},2000); | |
}catch(e){ | |
Ti.API.info(e); | |
} | |
}; | |
xhr.onerror = function(e){ | |
alert(e); | |
}; | |
// get my test json | |
xhr.open('GET', 'https://s3.amazonaws.com/pedruqui/tests/hotels.json'); | |
xhr.send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment