通过Http post方式发送json,然后获取返回的json

思路:将发送的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

 

 

 

你可能感兴趣的:(Flex)