[发布] 使用Flex HTTPService与TinyURL API创建的网址缩短小工具

Categories: Flex; Tagged with: ; @ October 16th, 2010 12:33

TinyURL 提供了极其简单易用的网址缩短API.  使用”http://tinyurl.com/api-create.php?url=http://liguoliang.com/“即可获得缩短链接. 这里是使用Flex制作的一个网址缩短小工具:

主要代码供热爱学习的同学们查阅: 主要是HTTPService

		/** On short url button.*/
		protected function onButtonClick(event:MouseEvent):void {
			CursorManager.setBusyCursor();
			var currentUrlRaw:String = StringUtils.trim(textURLRaw.text);

			var service:HTTPService = new HTTPService();
			service.addEventListener(ResultEvent.RESULT, onExecuteSuccess);
			service.addEventListener(FaultEvent.FAULT, onExecuteError);

			service.method = URLRequestMethod.GET;
			// Like : http://tinyurl.com/api-create.php?url=http://liguoliang.com/
			var serviceUrl:String = "http://tinyurl.com/api-create.php?url=" + currentUrlRaw;
			service.url = encodeURI(serviceUrl);
			service.resultFormat = HTTPService.RESULT_FORMAT_TEXT;

			service.send();
		}

		private var lastShortUrl:String;
		/** on API call success. */
		protected function onExecuteSuccess(e:ResultEvent):void {
			CursorManager.removeBusyCursor();
			lastShortUrl = e.result.toString();
			textInfo.text = "短网址生成成功:" + lastShortUrl + "(点此复制->)";
			buttonCopy.enabled = true;
		}

		/** on fault. */
		protected function onExecuteError(e:FaultEvent):void {
			CursorManager.removeBusyCursor();
			Alert.show("Error: " + e.toString());
		}

See: http://www.richardcastera.com/2009/05/09/creating-a-tinyurl-with-tinyurl-api/

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.