`
flyinglife
  • 浏览: 130777 次
社区版块
存档分类
最新评论

flex httpservice 交互方式

    博客分类:
  • flex
阅读更多
转载:http://cs.ntsky.com/flex3-java-json.html

 

Flex在做界面开发的时候有很多优势,Flex+java的组合也越来越流行,在整合开发的过程中自然会遇到数据交互的问题,下面介绍Flex3+Java整合使用json的情况。

一、Flex3中使用json需要corelib.swc

   http://www.adobe.com/cfusion/exchange/index.cfm?view=sn111&extid=1078469

   将corelib.swc拷贝到Flex安装目录的sdks\3.0.0\frameworks\libs下

二、Java中进行数据处理得到json格式的数据,部分代码

Java代码
Java代码 
/**    
 * 浏览器直接返回信息    
 *     
 * @param content 内容    
 * @param character 编码    
 * @throws IOException    
 */     
protected void out(String content,String character) throws IOException {       
    HttpServletResponse response = ServletActionContext.getResponse();      
    response.setContentType("text/html;charset="+character);       
    PrintWriter out = response.getWriter();      
    out.print(content);      
    out.flush();      
    out.close();      
}      
     
    try{       
        Author author = authorService.getAuthorByName(username);      
        if( author==null ){       
            JSONObject json = new JSONObject();       
            json.put("status","0");       
            json.put("message","用户名不存在");       
            super.out(json.toString());       
        }      
        if(author.getPassword().equals(MD5.md5(password))){       
            session.put(Symbols.SESSION_AUTHOR, author);      
            JSONObject json = new JSONObject();       
            json.put("status","1");       
            json.put("message","登录成功");       
            super.out(json.toString());                    
        }      
        else{       
            JSONObject json = new JSONObject();       
            json.put("status","0");       
            json.put("message","密码错误");       
            super.out(json.toString());            
        }      
              
    }      
    catch(ServiceException se){       
        JSONObject json = new JSONObject();       
        json.put("status","0");       
        json.put("message","登录失败");       
        super.out(json.toString());        
        throw new Exception(se);       
    } finally {       
    }             
    return NONE;      

/**  
 * 浏览器直接返回信息  
 *   
 * @param content 内容  
 * @param character 编码  
 * @throws IOException  
 */  
protected void out(String content,String character) throws IOException {    
    HttpServletResponse response = ServletActionContext.getResponse();   
    response.setContentType("text/html;charset="+character);    
    PrintWriter out = response.getWriter();   
    out.print(content);   
    out.flush();   
    out.close();   
}   
  
    try{    
        Author author = authorService.getAuthorByName(username);   
        if( author==null ){    
            JSONObject json = new JSONObject();    
            json.put("status","0");    
            json.put("message","用户名不存在");    
            super.out(json.toString());    
        }   
        if(author.getPassword().equals(MD5.md5(password))){    
            session.put(Symbols.SESSION_AUTHOR, author);   
            JSONObject json = new JSONObject();    
            json.put("status","1");    
            json.put("message","登录成功");    
            super.out(json.toString());                 
        }   
        else{    
            JSONObject json = new JSONObject();    
            json.put("status","0");    
            json.put("message","密码错误");    
            super.out(json.toString());         
        }   
           
    }   
    catch(ServiceException se){    
        JSONObject json = new JSONObject();    
        json.put("status","0");    
        json.put("message","登录失败");    
        super.out(json.toString());     
        throw new Exception(se);    
    } finally {    
    }          
    return NONE;      
三、Flex中请求URL得到json的数据,然后在script中解析json数据

XML/HTML代码
  Xml代码 
<?xml version="1.0" encoding="utf-8"?>     
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"        
    horizontalAlign="center"        
    verticalAlign="middle"        
    verticalGap="15"      
    horizontalGap="15">      
          
    <mx:Script>      
        <![CDATA[    
            import mx.collections.ArrayCollection;    
            import mx.rpc.events.ResultEvent;    
            import com.adobe.serialization.json.JSON;     
            import mx.controls.Alert;    
                
            [Bindable]    
            private var photoFeed:ArrayCollection;    
   
            private function doLogin():void {    
                trace("start send");    
                var params:Object = new Object();    
                params.username = username.text;    
                params.password = password.text;    
                loginCheck.send(params);    
                trace("end send");    
            }    
   
            private function doCancel():void {    
                username.text = "";    
                password.text = "";    
            }    
   
            private function loginHandler(event:ResultEvent):void {    
                    
                var rawData:String = String(event.result);    
                    
                //decode the data to ActionScript using the JSON API    
                //in this case, the JSON data is a serialize Array of Objects.    
                var arr:Array = (JSON.decode(rawData) as Array);    
                var dataArray:ArrayCollection = new ArrayCollection(arr);    
                //arraySize = dataArray.length;    
                        
                // info.text = event.result.toString();    
                var obj:Object = JSON.decode(rawData);    
               
                if(obj.status==1){    
                     var request:URLRequest = new URLRequest();    
                     request.method = URLRequestMethod.GET;    
                     request.url = "http://test.ntsky.com:8080/admin/posts.action";    
                     // request.    
                     var loader:URLLoader = new URLLoader();    
                     navigateToURL(request,"_self");    
                }    
                else{    
                    Alert.show(obj.message);    
                }    
            }    
                
         ]]>     
    </mx:Script>           
          
    <mx:HTTPService id="loginCheck"      
        url="http://test.ntsky.com:8080/admin/login.action"      
        resultFormat="text"      
               
        result="loginHandler(event)" />      
     
    <mx:Panel x="330" y="192" width="290" height="177" layout="absolute" title="用户登录" fontSize="12" horizontalAlign="center" verticalAlign="middle">      
        <mx:Form x="10" y="10" width="253" height="90">      
            <mx:FormItem label="用户名:">      
                <mx:TextInput id="username"/>      
            </mx:FormItem>      
            <mx:FormItem label="密  码 :">      
                <mx:TextInput id="password" displayAsPassword="true"/>      
            </mx:FormItem>      
        </mx:Form>      
        <mx:Button x="65" y="103" label="登录" click="doLogin()"/>      
        <mx:Button x="144" y="103" label="取消" click="doCancel()"/>      
    </mx:Panel>      
          
</mx:Application>     

<?xml version="1.0" encoding="utf-8"?>  
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"     
    horizontalAlign="center"     
    verticalAlign="middle"     
    verticalGap="15"   
    horizontalGap="15">   
       
    <mx:Script>   
        <![CDATA[  
            import mx.collections.ArrayCollection;  
            import mx.rpc.events.ResultEvent;  
            import com.adobe.serialization.json.JSON;   
            import mx.controls.Alert;  
              
            [Bindable]  
            private var photoFeed:ArrayCollection;  
 
            private function doLogin():void {  
                trace("start send");  
                var params:Object = new Object();  
                params.username = username.text;  
                params.password = password.text;  
                loginCheck.send(params);  
                trace("end send");  
            }  
 
            private function doCancel():void {  
                username.text = "";  
                password.text = "";  
            }  
 
            private function loginHandler(event:ResultEvent):void {  
                  
                var rawData:String = String(event.result);  
                  
                //decode the data to ActionScript using the JSON API  
                //in this case, the JSON data is a serialize Array of Objects.  
                var arr:Array = (JSON.decode(rawData) as Array);  
                var dataArray:ArrayCollection = new ArrayCollection(arr);  
                //arraySize = dataArray.length;  
                      
                // info.text = event.result.toString();  
                var obj:Object = JSON.decode(rawData);  
             
                if(obj.status==1){  
                     var request:URLRequest = new URLRequest();  
                     request.method = URLRequestMethod.GET;  
                     request.url = "http://test.ntsky.com:8080/admin/posts.action";  
                     // request.  
                     var loader:URLLoader = new URLLoader();  
                     navigateToURL(request,"_self");  
                }  
                else{  
                    Alert.show(obj.message);  
                }  
            }  
              
         ]]>  
    </mx:Script>        
       
    <mx:HTTPService id="loginCheck"   
        url="http://test.ntsky.com:8080/admin/login.action"   
        resultFormat="text"   
            
        result="loginHandler(event)" />   
  
    <mx:Panel x="330" y="192" width="290" height="177" layout="absolute" title="用户登录" fontSize="12" horizontalAlign="center" verticalAlign="middle">   
        <mx:Form x="10" y="10" width="253" height="90">   
            <mx:FormItem label="用户名:">   
                <mx:TextInput id="username"/>   
            </mx:FormItem>   
            <mx:FormItem label="密  码 :">   
                <mx:TextInput id="password" displayAsPassword="true"/>   
            </mx:FormItem>   
        </mx:Form>   
        <mx:Button x="65" y="103" label="登录" click="doLogin()"/>   
        <mx:Button x="144" y="103" label="取消" click="doCancel()"/>   
    </mx:Panel>   
       
</mx:Application>   
 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics