Http post 방식 으로 json 을 보 내 고 돌아 온 json 을 가 져 옵 니 다.

3560 단어 Flex
사고:보 낸 json 을 xml 로 변환 합 니 다.
 
예제 코드
 
package
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.ui.Keyboard;
	import flash.xml.XMLDocument;
	
	[SWF(width="500", height="200" )]
	public class JsonDemo extends Sprite
	{
		private var result:TextField;		
		private var cell_id:TextField;
		private var tip:TextField;
		
		private var request:URLRequest;
		private var loader:URLLoader;
		
		public function JsonDemo()
		{
			initUI();
			PostAndLoad();
		}
		
		private function initUI():void
		{
			result = new TextField();
			result.x = 10;
			result.y = 50;
			result.width = 480;
			result.height = 100;
			result.border = true;
			result.background = true;
			result.wordWrap = true;
			result.type = TextFieldType.DYNAMIC;
			
			cell_id = new TextField();
			cell_id.x = 390;
			cell_id.y = 10;
			cell_id.width = 100;
			cell_id.height = 20;
			cell_id.border = true;
			cell_id.background = true;
			cell_id.type = TextFieldType.INPUT;
			cell_id.text = "36526";//      
			
			tip = new TextField();
			tip.x = 260;
			tip.y = 10;
			tip.width = 160;
			tip.height = 20;
			tip.type = TextFieldType.DYNAMIC;
			tip.htmlText = "  Cell_ID   ";
			
			addChild(tip);
			addChild(cell_id);
			addChild(result);
			
			addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler)
		}
		
		//         
		private function KeyDownHandler(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case Keyboard.ENTER:
					PostAndLoad(parseInt(cell_id.text));
					cell_id.text = "";
					break;
			}
		}
		
		/**
		 *      xml json    xml json
		 * @param cell_id:int = 36526
		 */
		private function PostAndLoad(cell_id:int = 36526):void
		{
			var arr:Array = [];
			// cell_id      
			arr.push({
				"version": "1.1.0" ,
				"host": "maps.google.com",
				"access_token": "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe",
				"home_mobile_country_code": 460,
				"home_mobile_network_code":0,
				"address_language": "zh_CN",
				"radio_type": "gsm",
				"request_address": true ,
				"cell_towers":[
					{
						"cell_id":cell_id,
						"location_area_code":14556,
						"mobile_country_code":460,
						"mobile_network_code":0,
						"timing_advance":5555
					}
				]
			});
			var dataAsObject:Object = arr.shift();
			var dataAsString:String = JSON.encode(dataAsObject);
			trace(dataAsString);
			
			//       Json  
			var send_xml:XML = new XML(dataAsString);
			trace(send_xml.toXMLString());
			
			// Http  
			request = new URLRequest("http://www.google.com/loc/json");
			request.method = URLRequestMethod.POST;
			request.data = send_xml;
			
			//         
			loader = new URLLoader();
			loader.load(request);
			loader.addEventListener(Event.COMPLETE,LoaderHandler);//   
		}
		
		//          
		private function LoaderHandler(evt:Event):void
		{
			var load_xml:XML = new XML(evt.target.data);
			trace("     :" + load_xml.toXMLString());
			trace("------------  " + load_xml);
			result.htmlText = load_xml.toXMLString();
		}
		
	}
}

 
 
사용 하 는 라 이브 러 리:as3corelib
 
 

좋은 웹페이지 즐겨찾기