Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 13th, 2012  |  syntax: JavaScript  |  size: 0.72 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function shorten_url_google(longurl, callback) {
  2.         var XHR = Titanium.Network.createHTTPClient();
  3.  
  4.         XHR.open("POST", "https://www.googleapis.com/urlshortener/v1/url");
  5.         XHR.setRequestHeader('Content-Type', 'application/json');
  6.         XHR.onload = function() {
  7.                 var shorturl = false;
  8.                 try {
  9.  
  10.                         var response = JSON.parse(XHR.responseText);
  11.                         if( typeof response === 'object' && response.id) {
  12.                                 shorturl = response.id;
  13.                         }
  14.                 } catch(e) {
  15.                         Ti.API.info("Exception while shortening " + longurl);
  16.                 }
  17.                 callback(shorturl, longurl);
  18.         };
  19.         XHR.onerror = function() {
  20.  
  21.                 Ti.API.info("Error while shortening " + longurl + " (" + XHR.status + ")");
  22.                 callback(false);
  23.         };
  24.         XHR.send(JSON.stringify({
  25.                 longUrl : longurl
  26.         }));
  27. }