Flex4之与后台服务器通信方式:URLRequest+URLLoader【JAVA方式】

这次说的URLRequest是ActionScript和服务端通信的最基本方式

要实现ActionScript与服务端交互, 首先必须知道以下几个类: URLRequest类 记录请求服务端的URL信息, 给URLLoader进行发送操作.    URLVariables类 保存向服务端请求所需的参数, 一般用于POST方式传输,GET方式传输不需要使用. URLRequestMethod类 该类只有两个静态常量值, 一个是POST, 一个是GET, 可以把它当作是一个枚举类型, 用于指定URL请求时使用POST方式或是GET方式. URLLoader类 真正与服务端打交道的类, 通过load方法把一个Reuqest发送给服务端, 在load之前必须给该对象添加一个Complete事件的监听器,接收服务器返回的信息. 传输过程中有可能产生IO错误, 有必要时可以为该对象添加IOError监听器.

 

例子: var request: URLRequest = new URLRequest("http://localhost");

//默认为URLRequestMethod.GET, 若使用GET方式可以不指定 request.method = URLRequestMethod.POST;

//创建一个参数变量对象,用于保存所有向服务发送的参数数据, GET方式可以不使用 var vars: URLVariables = new URLVariables (); vars["varName"] = varValue; //把参数键,值对放到vars对象中. vars["var2Name"] = var2Value; request.data = vars;  //把参数对象放进Request对象中作为参数

// 通过request对象创建一个loader, var loader: URLLoader = new URLLoader(request);

 

 

事件及返回值的处理

loader.addEventListener( //给loader对象添加完成时的监听器. Event.COMPLETE, function(e: Event):void  //临时定义一个监听器 {   //服务端返回的数据是保存在loader的data里,通过以下语句可以得到   trace(URLLoader(e.target).data );//在监听器里处理服务端返回的数据   //complete   // process the data; } );

loader.addEventListener( //IOError监听器 IOErrorEvent.IO_ERROR, //临时监听器,IO错误的处理在该函数中处理 function(e: IOErrorEvent): void {/* process the err;*/} ); loader.load(request);//把request对象发送到服务端.当请求完成,则会自动触发COMPLETE事件

下面写一个具体的代码示例

mxml的具体代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<span style="font-size: medium;"><?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.utils.URLUtil;
            protected function myFlex_resultHandler(event:ResultEvent):void
            {
                var name:String=event.result as String;
                Alert.show(name);
            }
             
//          protected function btnSend_clickHandler(event:MouseEvent):void
//          {
//             
//              myFlex.sayHello(txtName.text);
//          }
 
            protected function button1_clickHandler(event:MouseEvent):void
            {
                var v:URLVariables = new URLVariables("name=jacky&age=35&address=HongKong China"); 
                /*
                也可以是这种方式
                var vars: URLVariables = new URLVariables ();
                vars["varName"] = varValue; //把参数键,值对放到vars对象中.
                vars["var2Name"] = var2Value;
                */ 
                var r:URLRequest = new URLRequest(); 
                r.url = "http://localhost:8080/Flex4BlazeDSWeb/test.do"
                r.method = URLRequestMethod.POST; 
                r.data = v; 
         
                var l:URLLoader = new URLLoader();         
                l.load(r); 
                     
                l.addEventListener(Event.COMPLETE,txtCompleteHandler); //注册请求完成后 响应获取数据事件方法 
                //labMsg.alpha = 1; 
            }
             
            private function txtCompleteHandler(e:Event):void 
            
 
                var l:URLLoader = URLLoader(e.target); 
                /* URLUtil的stringToObject方法解释
                从字符串返回对象。字符串包含 name=value 对,它们成为返回对象的动态属性。这些属性对由指定的 separator 分隔。该方法可转换数字、布尔值、数组(由“[]”定义)以及子类(由“{}”定义)。默认情况下,%XX 格式的 URL 模式将转换为相应的字符串字符。
                例如:
                var s:String = "name=Alex;age=21";
                var o:Object = URLUtil.stringToObject(s, ";", true);       
                返回对象:{ name: "Alex", age: 21 }。
                */
                //直接获取返回值内容:  URLLoader(e.target).data
                Alert.show(l.data);
                var o:Object = URLUtil.stringToObject(l.data,";",true); //使用URLUtil对将数据进行反序列化以方便使用 
                 
                txtName.text = o.name; 
                txtAge.text = o.age; 
                txtAddress.text = o.address; 
                //labMsg.alpha = 0; 
            
 
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!--请把不可视的组件元素例如服务的,值对象的放在声明里面  Place non-visual elements (e.g., services, value objects) here
        <s:RemoteObject id="myFlex" destination="mytest" result="myFlex_resultHandler(event)"  />-->
    </fx:Declarations>
    <s:Panel x="77" y="35" width="198" height="231" title="www.accptecg.com flex4">
        <s:TextInput x="47" y="102" id="txtAddress"/>
        <s:TextInput x="47" y="72" id="txtAge"/>
        <s:TextInput x="47" y="37" id="txtName"/>
        <!--<s:Button x="168" y="38" label="发送" id="btnSend" click="btnSend_clickHandler(event)"/>-->
        <s:Button x="47" y="154" label="按钮" click="button1_clickHandler(event)"/>
        <s:Label x="0" y="47" text="Name"/>
        <s:Label x="4" y="81" text="Age" height="13"/>
        <s:Label x="4" y="115" text="Address" height="13"/>
    </s:Panel>
</s:Application>
</span>

 

处理程序为

Java代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<span style="font-size: medium;">public class TestServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
 
            throws ServletException, IOException {
          
       this.doPost(request, response);
      
    }
 
 
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         //获取URLResuest请求回的参数 返回数据用;分隔以方便Flex对数据进行序列化 
           String rs =new String("name="+request.getParameter("name")+";age="+request.getParameter("age")+";address="+request.getParameter("address"));
             System.out.println(request.getParameter("name"));
            response.setContentType("text/plain");
            System.out.println(rs);
            response.getWriter().write(rs);
     
            //request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
 
     
}</span>

  

好了,这次的URLRequest方式就到此为止了

Flex4之与后台服务器通信方式:URLRequest+URLLoader【JAVA方式】,布布扣,bubuko.com

Flex4之与后台服务器通信方式:URLRequest+URLLoader【JAVA方式】

上一篇:list-style-image 设置li不起作用问题


下一篇:Effective Java 32 Use EnumSet instead of bit fields