nezilabテスト 短縮URL篇
短縮URLのテスト
/**
* Copyright nezilab ( http://wonderfl.net/user/nezilab )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ikFk
*/
//短縮URLのテスト
package
{
import flash.net.*;
import flash.display.*;
import flash.events.*;
import flash.external.*;
import flash.text.*;
import flash.external.ExternalInterface;
import com.bit101.components.*;
public class NezilabTest extends Sprite
{
private var phpURL :String = "http://tinyurl.com/api-create.php?url=";
private var phpcommu :PhpCommunication;
private var tf :TextField;
private var urltf :InputText;
public function NezilabTest()
{
// write as3 code here..
init();
}
private function init():void
{
phpcommu = new PhpCommunication();
phpcommu.addEventListener(CommunicationEvent.COMMUNICATION_END , onPhpComp);
phpcommu.addEventListener(CommunicationEvent.COMMUNICATION_ERROE , onPhpError);
tf = new TextField();
tf.text = "ここに結果が入る";
tf.width = 465;
tf.height = 26;
addChild(tf)
urltf = new InputText(this, 350, 10, "");
var submit : PushButton = new PushButton(this, 350, 40, "Submit", onSubmit);
}
private function onSubmit(e:MouseEvent):void
{
var nURL:String = urltf.text;
phpcommu.startCommunication(phpURL+nURL , {});
}
private function onPhpComp(e:CommunicationEvent):void
{
tf.text = e.tempObj as String;
}
private function onPhpError(e:CommunicationEvent):void
{
tf.text = "PHPとあくせすできなかったよ。";
}
}
}
import flash.net.*;
import flash.events.*;
import flash.display.*;
import flash.external.*;
class PhpCommunication extends EventDispatcher
{
private var loader :URLLoader;
/**
*接続
* @param $url flashvarsで読み込んだurl?
*/
public function startCommunication(url:String , variables:Object):void
{
var cache_buster:Number = Math.floor(Math.random() * 10000);
var request:URLRequest = new URLRequest ( url );
request.method = URLRequestMethod.POST;
var urlVariables:URLVariables = new URLVariables();
for (var i:String in variables)
{
urlVariables[i] = variables[i];
}
request.data = urlVariables;
loader = new URLLoader (request);
loader.dataFormat = URLLoaderDataFormat.TEXT;
//↓データを送った後、値を受け取る際に使用
loader.addEventListener(Event.COMPLETE, onCompleteCommunication);
loader.addEventListener(IOErrorEvent.IO_ERROR , onError);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR , onError);
}
private function onCompleteCommunication(e : Event):void
{
var ndata:String = e.target.data as String;
loader.removeEventListener(Event.COMPLETE, onCompleteCommunication);
loader.removeEventListener(IOErrorEvent.IO_ERROR , onError);
loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR , onError);
dispatchEvent(new CommunicationEvent(CommunicationEvent.COMMUNICATION_END , ndata));
}
/*
* エラー返されたときの処理
*/
private function onError(e:Event):void
{
loader.removeEventListener(Event.COMPLETE, onCompleteCommunication);
loader.removeEventListener(IOErrorEvent.IO_ERROR , onError);
loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR , onError);
dispatchEvent(new CommunicationEvent(CommunicationEvent.COMMUNICATION_ERROE));
}
}
import flash.events.Event;
class CommunicationEvent extends Event
{
//LOAD
public static const COMMUNICATION_ERROE :String = "COMMUNICATION_ERROE";
public static const COMMUNICATION_END :String = "COMMUNICATION_END";
public var tempObj :Object;
public function CommunicationEvent(type:String, tempObj:Object = null )
{
super( type );
this.tempObj = tempObj;
}
public override function clone():Event
{
return new CommunicationEvent(type, tempObj);
}
}