RPC通信之HTTPService(Java-flex)

本文介绍了一个使用Flex和Java实现的简单应用案例,该案例展示了如何在客户端和服务端之间通过JSON格式的数据进行交互。具体包括Flex端的数据封装、发送及解析,以及Java端的数据接收、解析和响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!-- 一、 Flex 封装/发送、接收/解析 Json -->
<?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:Style source="Regist.css"/>

    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            [bindable]
            var id1:String = "007";
            [bindable]
            var email1:String = "@";
            [bindable]
            var date1:String = "2000-1-1";
            protected function button1_clickHandler(event:MouseEvent):void
            {
                /**
                 * 1.将参数封装成Object对象  2.将Object对象转换成json串  3.将json赋值给参数   4.传递参数
                 */
                var user:Object = {};
                user.id = num.text;
                user.email = email.text;
                user.date = date.text;
                var jsonStr:String = JSON.stringify(user);
                var params:URLVariables = new URLVariables();
                params.user = jsonStr;
                http.send(params);
            }
            /**
             * 获取后端传过来的json对象并进行解析
             */
            protected function http_resultHandler(event:ResultEvent):void
            {
                var user:Object = JSON.parse(event.result.toString());
                java_id.text = user.id;
                java_email.text = user.email;
                java_date.text = user.date;
            }

            protected function http_faultHandler(event:FaultEvent):void
            {
                Alert.show("error");
            }

        ]]>
    </fx:Script>

    <fx:Declarations>
        <s:HTTPService  id="http"
                        method="POST"
                        useProxy="false"
                        showBusyCursor="true"
                        resultFormat="e4x"
                        url="http://localhost:8080/test/RegistAction"
                        result="http_resultHandler(event)"
                        fault="http_faultHandler(event)"
                        />
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
    </fx:Declarations>
    <s:Label x="152" y="71" height="22" text="员工编号"/>
    <s:Label x="152" y="108" height="22" text="邮        箱"/>
    <s:TextInput id="num" text="{id1}" x="220" y="71"/>
    <s:TextInput id="email" text="{email1}" x="220" y="108"/>
    <s:Label x="151" y="145" height="22" text="注册日期"/>
    <s:TextInput id="date" text="{date1}" x="219" y="145"/>
    <s:Button x="151" y="185" width="236" height="27" label="注        册" chromeColor="#1747F8"
              click="button1_clickHandler(event)"/>
    <s:TextInput id="java_id" x="515" y="69"/>
    <s:TextInput id="java_date" x="515" y="150"/>
    <s:TextInput id="java_email" x="515" y="108"/>
    <s:Label x="423" y="78" text="员工编号:"/>
    <s:Label x="427" y="160" text="注册日期:"/>
    <s:Label x="427" y="115" text="邮       箱:"/>
    <s:Label x="476" y="193" width="161" height="34" text="后端传输数据展示区"/>
</s:Application>

<!-- 二、 Java 封装/发送、接收/解析 Json -->
protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /**
         * 1.从flex前端接收json串  2.将json串 转换为jsonObject 3.将jsonObject转换为JavaBean
         */
        String jsonStr = request.getParameter("user");
        JSONObject jo = JSONObject.fromObject(jsonStr);
        UserEntry user = new UserEntry();
        user.setId((String) jo.get("id"));
        user.setEmail((String) jo.get("email"));
        String dateStr = (String) jo.get("date");
        Date dateObj = Date.valueOf(dateStr);
        user.setDate(dateObj);
        /**
         * 1.将user对象封装成json对象  2.json对象 将传递给Flex
         */
        JSONObject resultJO = new JSONObject(); 
        dateStr = user.getDate().toString();
        resultJO.put("id", user.getId());
        resultJO.put("email", user.getEmail());
        resultJO.put("date",dateStr);
        PrintWriter out = response.getWriter();
        out.print(resultJO);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值