Flex之HTTPService组件调用

1、采用<s:HTTPService>标签来实现:

<?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" >

  <s:Label x="61" y="138" text="HttpService:"/>
  <s:TextInput x="152" y="128" id="http_txt"/>
  <s:Button x="289" y="129" id="btn_login" label="发送" click="button2_clickHandler(event)"/>
  <s:Label x="383" y="138" id="http_result"/>

  <fx:Script>
    <![CDATA[
      import mx.rpc.events.ResultEvent;
      import mx.controls.Alert;
      import mx.rpc.events.FaultEvent;
      //HttpService的形式访问Java服务器
      protected function button2_clickHandler(event:MouseEvent):void
      {
        btn_login.addEventListener(MouseEvent.CLICK,login);
        //加载HTTPService的返回监听
        httpSer.addEventListener(ResultEvent.RESULT,httpSerResultHandler);
        httpSer.addEventListener(FaultEvent.FAULT,httpSerFaultHandler);
      }
      private function login(event:MouseEvent):void{
        httpSer.send();//发送请求
      }
      //返回成功事件
      private function httpSerResultHandler(event:ResultEvent):void{
        Alert.show(event.result.toString(),"登录提示");
      }
      //返回失败事件
      private function httpSerFaultHandler(event:FaultEvent):void{
        Alert.show(event.fault.message as String,"登录提示");
      }
    ]]>
  </fx:Script>
  <fx:Declarations>
    <s:HTTPService id="httpSer" url="http://localhost:8080/html/FlexServlet" method="POST">
      <s:request>
        <!--需要发送到服务器的参数名,及值,接收参数名时必须一致i。其中name表示参数名称。-->
        <name>{http_txt.text}</name>
      </s:request>
    </s:HTTPService>
  </fx:Declarations>
</s:Application>

2、采用创建HTTPService对象:

<?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" >

  <s:Label x="61" y="138" text="HttpService:"/> 
  <s:TextInput x="152" y="128" id="http_txt"/> 
  <s:Button x="289" y="129" id="btn_login" label="发送" click="button2_clickHandler(event)"/> 
  <s:Label x="383" y="138" id="http_result"/> 

  <fx:Script> 
    <![CDATA[ 
      import mx.rpc.events.ResultEvent;
      import mx.controls.Alert;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.http.HTTPService;

      protected function button2_clickHandler(event:MouseEvent):void 
      { 
        

        var http:HTTPService = new HTTPService();
        http.url = "http://localhost:8080/html/FlexServlet0?name="+http_txt.text;
        http.send();
        http.addEventListener(ResultEvent.RESULT,httpSerResultHandler);
        http.addEventListener(FaultEvent.FAULT,httpSerFaultHandler);

      } 
      private function login(event:MouseEvent):void{
        httpSer.send();//发送请求
      }
      //返回成功事件
      private function httpSerResultHandler(event:ResultEvent):void{
        Alert.show(event.result.toString(),"登录提示");
      }
      //返回失败事件
      private function httpSerFaultHandler(event:FaultEvent):void{
        Alert.show(event.fault.message as String,"登录提示");
      }
    ]]> 
  </fx:Script> 
  <fx:Declarations>
    
  </fx:Declarations>
</s:Application>

3、在JavaWeb项目中新建Servlet(FlexServlet) :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		  String name = request.getParameter("name");   

              response.setCharacterEncoding("UTF-8");   

              PrintWriter pw = response.getWriter();   

              pw.write("你好["+name+"]这是来自Httpservice的消息...当前Session是:"+request.getSession());   

              pw.close();   

	}

  

你可能感兴趣的:(service)