as对象与java对象的转换

java对象不能直接转换为flex对象,所以需要创建一个as对象对应java对象,并且以[RemoteClass(alias="hello.User")] 
来指定对应的java对象类。下面我就以一个简单的例子来诠释交互

此例子在上一个例子中稍作改动即可应用

java类

User代码 复制代码  收藏代码
  1. package hello;   
  2.   
  3. public class User {   
  4.   
  5.     private long id;   
  6.     private String name;   
  7.     private String password;   
  8.     private long age;   
  9.     public long getId() {   
  10.         return id;    
  11.     }   
  12.     public void setId(long id) {   
  13.         this.id = id;   
  14.     }   
  15.     public String getPassword() {   
  16.         return password;   
  17.     }   
  18.     public void setPassword(String password) {   
  19.         this.password = password;   
  20.     }   
  21.     public long getAge() {   
  22.         return age;   
  23.     }   
  24.     public void setAge(long age) {   
  25.         this.age = age;   
  26.     }   
  27.     public String getName() {   
  28.         return name;   
  29.     }   
  30.     public void setName(String name) {   
  31.         this.name = name;   
  32.     }   
  33.        
  34. }  
package hello;

public class User {

	private long id;
	private String name;
	private String password;
	private long age;
	public long getId() {
		return id; 
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public long getAge() {
		return age;
	}
	public void setAge(long age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

 

 as

 

As代码 复制代码  收藏代码
  1. package   
  2. {   
  3. [RemoteClass(alias="hello.User")]     
  4.     public class User    
  5.     {   
  6.      public  var name:String;   
  7.      public  var password:String;   
  8.      public var age:int;   
  9.      public var id:int;   
  10.         public function User()   
  11.         {   
  12.         }   
  13.     }    
  14. }  
package
{
[RemoteClass(alias="hello.User")]  
	public class User 
	{
     public  var name:String;
     public  var password:String;
     public var age:int;
     public var id:int;
		public function User()
		{
		}
	} 
}

 

 mxml

 

Mxml代码 复制代码  收藏代码
  1. <?xml   version = "1.0"   encoding = "utf-8" ?>         
  2. <mx:Application   xmlns:mx = "http://www.adobe.com/2006/mxml" >         
  3. <mx:Script >         
  4.   <![CDATA[    
  5.   import mx.rpc.events.FaultEvent;       
  6.   import mx.rpc.events.ResultEvent;       
  7.   import mx.controls.Alert;   
  8.       [Bindable]       
  9.       private var helloResult:String;   
  10.          [Bindable]          
  11.       private var user:User = new User();       
  12.       private function sayHelloTo():void {    
  13.         user.name = inputText.text;    
  14.         ro.sayHelloTos(user);      
  15.         ro.addEventListener(ResultEvent.RESULT,resultHandler);   
  16.         }       
  17.            
  18.        private function resultHandler(event:ResultEvent):void {       
  19.                  user  = event.result as User;   
  20.                 if(null!=user){    
  21.                     inputName.text=user.name;   
  22.                     helloResult = user.name;   
  23.                 }else{    
  24.                     Alert.show("空值");   
  25.                 }    
  26.              } ]]>       
  27.  </mx:Script >          
  28.          <mx:RemoteObject id="ro" destination="helloworld" />         
  29.          <mx:RemoteObject id="users" destination="user">   
  30.             <mx:method name="user">   
  31.             </mx:method>             
  32.           </mx:RemoteObject>   
  33.          <mx:HBox width = "100%">          
  34.            <mx:TextInput id = "inputText" name="user.name"/>         
  35.            <mx:TextInput id="inputName" name="user.name"/>   
  36.            <mx:Button label = "Submit"   click = "sayHelloTo()" />         
  37.          </mx:HBox >   
  38.          <mx:Label text = "{helloResult}" />   
  39.     
  40.   </mx:Application>        
  41.        
  42.       
<?xml   version = "1.0"   encoding = "utf-8" ?>      
<mx:Application   xmlns:mx = "http://www.adobe.com/2006/mxml" >      
<mx:Script >      
  <![CDATA[ 
  import mx.rpc.events.FaultEvent;    
  import mx.rpc.events.ResultEvent;    
  import mx.controls.Alert;
      [Bindable]    
      private var helloResult:String;
         [Bindable]       
      private var user:User = new User();    
      private function sayHelloTo():void { 
      	user.name = inputText.text; 
        ro.sayHelloTos(user);   
        ro.addEventListener(ResultEvent.RESULT,resultHandler);
        }    
        
       private function resultHandler(event:ResultEvent):void {    
                 user  = event.result as User;
                if(null!=user){ 
                	inputName.text=user.name;
                	helloResult = user.name;
                }else{ 
                	Alert.show("空值");
                } 
             } ]]>    
 </mx:Script >       
         <mx:RemoteObject id="ro" destination="helloworld" />      
         <mx:RemoteObject id="users" destination="user">
            <mx:method name="user">
            </mx:method>         	
          </mx:RemoteObject>
         <mx:HBox width = "100%">       
           <mx:TextInput id = "inputText" name="user.name"/>      
           <mx:TextInput id="inputName" name="user.name"/>
           <mx:Button label = "Submit"   click = "sayHelloTo()" />      
         </mx:HBox >
         <mx:Label text = "{helloResult}" />
 
  </mx:Application>     
	
	

 

  java服务类

 

Java代码 复制代码  收藏代码
  1. package hello;   
  2.   
  3. public class HelloWorld {   
  4.   
  5.     public HelloWorld(){   
  6.     }   
  7.     public User sayHelloTos(User user) {   
  8.         System.out.println(user.getName());   
  9.         return user;      
  10.     }   
  11. }    
  12.    
package hello;

public class HelloWorld {

	public HelloWorld(){
	}
	public User sayHelloTos(User user) {
		System.out.println(user.getName());
		return user;   
	}
} 
 

   通信配置xml

 

 

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <service id="remoting-service"    
  3.     class="flex.messaging.services.RemotingService">  
  4.   
  5.     <adapters>  
  6.         <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>  
  7.     </adapters>  
  8.   
  9.     <default-channels>  
  10.         <channel ref="my-amf"/>  
  11.     </default-channels>  
  12.        
  13.   <destination id="helloworld">           
  14.       <properties>          
  15.           <source>hello.HelloWorld</source>    
  16.         </properties>          
  17.   </destination >    
  18.   
  19. </service>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值