Created
December 21, 2012 15:54
-
-
Save anonymous/4353617 to your computer and use it in GitHub Desktop.
Função para reduzir url com a API do google
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
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