Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save martinbowling/182022 to your computer and use it in GitHub Desktop.
Save martinbowling/182022 to your computer and use it in GitHub Desktop.
//Make XHR request to post picture
function postPic(pic) {
if (Titanium.Network.NETWORK_NONE) {
var a = Titanium.UI.createAlertDialog({
title:'We\'re Sorry...',
message:'Snapost cannot detect a network connection. Are you connected to the internet?'
});
}
else {
var query = 'https://twitpic.com/api/uploadAndPost';
var xhr = Titanium.Network.createHTTPClient();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
//Check the response to see if we're okay
//TwitPic API reference: http://twitpic.com/api.do
var parser = new DOMParser();
var doc = parser.parseFromString(this.responseText, "text/xml");
$(doc).find("rsp").each(function() {
var stat = $(this).attr('stat'); //We only get this on an error
if (stat == null || stat !== 'fail') {
reset(function() {
$("#posting").fadeOut(function() {
$("#success").fadeIn();
});
setTimeout(function() {
$("#success").fadeOut(function() {
$("#post-button").fadeIn();
});
},4000);
});
}
else {
reset(function() {
$(doc).find("err").each(function() {
$("#error-message").html($(this).attr('msg'));
$("#posting").fadeOut(function() {
$("#error").fadeIn();
});
setTimeout(function() {
$("#error").fadeOut(function() {
$("#post-button").fadeIn();
});
},8000);
});
});
}
});
}
};
xhr.open('POST',query);
xhr.send({
media:pic,
username: Titanium.App.Properties.getString('username'),
password: Titanium.App.Properties.getString('password'),
message: message.value
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment