Flex的通信方式(四)——HTTPService_2

转载自: http://cs.ntsky.com/flex3-java-json.html
在转载源的基础上进行分析说明



1、Flex中请求URL得到json的数据,然后在script中解析json数据
其中用的是HTTPService通信方式 和 navigateToURL直接访问URL
<?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>   



2、Java中进行数据处理得到json格式的数据,部分代码
/**   
 * 浏览器直接返回信息   
 *    
 * @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;    



补充:JSON快速入门简易介绍: http://baike.baidu.com/view/136475.htm?fr=ala0

你可能感兴趣的:(json,xml,Flex,Adobe,actionscript)