Skip to content

Instantly share code, notes, and snippets.

Created December 21, 2012 15:54
Show Gist options
  • Save anonymous/4353617 to your computer and use it in GitHub Desktop.
Save anonymous/4353617 to your computer and use it in GitHub Desktop.
Função para reduzir url com a API do google
function shorten_url(url,callback) {
var XHR = Titanium.Network.createHTTPClient({
onload: function () {
try {
shorturl = JSON.parse(this.responseText);
shorturl = shorturl.id;
} catch(e) {
shorturl = false;
}
if (typeof(callback)=='function') {
callback(shorturl,url);
}
return shorturl.id;
},
onerror: function(e) {
if (typeof(callback)=='function') {
callback(false);
}
return false;
},
timeout: 8000,
});
XHR.open("POST", "https://www.googleapis.com/urlshortener/v1/url");
XHR.setRequestHeader('Content-Type', 'application/json');
XHR.send('{"longUrl": "'+url+'"}');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment