Skip to content

Instantly share code, notes, and snippets.

@pec1985
Created April 19, 2011 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pec1985/929634 to your computer and use it in GitHub Desktop.
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
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