Actionscript与Java数据类型转换

    From: http://ioryioryzhan.iteye.com/blog/204230

    使用BlazeDS时与LCDS的转换方式类似。


    使用lcds时,由于直接可以和java打交道,可以不用通过xml或者其他方式通信,用起来真的很方便,听说效率非常好,虽然不知道怎么来测试这效率,但还是很乐意去用它。数据从前台传到后台,后台处理后又将新数据传到前台,有人说web编程便是处理字符串,并不是没有道理的。
    最先遇到的问题便是actionscript与java数据类型转换,比如在actionScript中定义一个int型的变量,传到后台时会自动对应为一个java的int型变量。但数据类型那么多,还真要好好列一列,当然不是自己去一个一个试,官方的文档里早就写好了。一个是actionScript 到java,一个是java到actionScipt,两张表:

从actionScript到java:

**************************************************************************

ActionScript type (AMF 3)

Deserialization to Java

Supported Java type binding

Array (dense)

java.util.List

java.util.Collection, Object[ ] (native array)

If the type is an interface, it is mapped to the following interface implementations

  • List becomes ArrayList
  • SortedSet becomes TreeSet
  • Set becomes HashSet
  • Collection becomes ArrayList

A new instance of a custom Collection implementation is bound to that type.

Array (sparse)

java.util.Map

java.util.Map

Boolean

String of "true" or "false"

java.lang.Boolean

Boolean, boolean, String

flash.utils.ByteArray

byte []

 

flash.utils.IExternalizable

java.io.Externalizable

 

Date

java.util.Date

(formatted for Coordinated Universal Time (UTC))

java.util.Date, java.util.Calendar, java.sql.Timestamp, java.sql.Time, java.sql.Date

int/uint

java.lang.Integer

java.lang.Double,java.lang.Long, java.lang.Float, java.lang.Integer, java.lang.Short,java.lang.Byte, java.math.BigDecimal, java.math.BigInteger, String,primitive types of double, long, float, int, short, byte

null

null

primitives

Number

java.lang.Double

java.lang.Double,java.lang.Long, java.lang.Float, java.lang.Integer, java.lang.Short,java.lang.Byte, java.math.BigDecimal, java.math.BigInteger, String, 0(zero) if null is sent, primitive types of double, long, float, int,short, byte

Object (generic)

java.util.Map

Ifa Map interface is specified, creates a new java.util.HashMap forjava.util.Map and a new java.util.TreeMap for java.util.SortedMap.

String

java.lang.String

java.lang.String,java.lang.Boolean, java.lang.Number, java.math.BigInteger,java.math.BigDecimal, char[], any primitive number type

typed Object

typed Object

when you use [RemoteClass] metadata that specifies remote classname. Bean type must have a public no args constructor.

typed Object

undefined

null

null for Object, default values for primitives

XML

org.w3c.dom.Document

org.w3c.dom.Document

XMLDocument

(legacy XML type)

org.w3c.dom.Document

org.w3c.dom.Document

 

Youcan enable legacy XML support for the XMLDocument type on any channeldefined in the services-config.xml file. This setting is only importantfor sending data from the server back to the client; it controls howorg.w3c.dom.Document instances are sent to ActionScript. For moreinformation, see

**************************************************************************

从java到actionScript:

**************************************************************************

Java type ActionScript type (AMF 3)
java.lang.String String
java.lang.Boolean, boolean Boolean
java.lang.Integer int
If i < 0xF0000000 || i > 0x0FFFFFFF, the value is promoted to Number.
java.lang.Short int
If i < 0xF0000000 || i > 0x0FFFFFFF, the value is promoted to Number.
java.lang.Byte int
If i < 0xF0000000 || i > 0x0FFFFFFF, the value is promoted to Number.
java.lang.Byte[] flash.utils.ByteArray
java.lang.Double Number
java.lang.Long Number
java.lang.Float Number
java.lang.Character String
java.lang.Character[] String
java.util.Calendar Date
Datesare sent in the Coordinated Universal Time (UTC) time zone. Clients andservers must adjust time accordingly for time zones.
java.util.Date Date
Dates are sent in the UTC time zone. Clients and servers must adjust time accordingly for time zones.
java.lang.Object (other than previously listed types) Typed Object
Objects are serialized using Java Bean introspection rules. Fields that are static, transient, or nonpublic are excluded.
java.util.Collection mx.collection.ArrayCollection
java.lang.Object[] Array
java.util.Map Object (untyped)
InFlex 1.5, java.util.Map was sent as an associative or ECMA Array. Thisis no longer a recommended practice. You can enable legacy Map supportto associative Arrays, but Adobe recommends against doing this. Formore information, see Providing legacy AMF serialization on a channel
java.util.Dictionary Object (untyped)
org.w3c.dom.Document XML object
Youcanenable legacy XML support for the XMLDocument type on anychanneldefined in the services-config.xml file. For more information,seeProviding legacy AMF serialization on a channel.
null null
Other classes that extend java.lang.Object Object (typed)
Objects are serialized using Java Bean introspection rules. Fields that are static, transient, or nonpublic are excluded.

 

 

**************************************************************************

    这里面我用得最多的还是一些基本数据类型如:int ,Number,String等,其次便是ArrayCollection,上面的表里居然没有,ft,它对应的是java的list。当要传送大量数据时,当然不能一个数据一个数据地传,在as这边定义一个类A,同样在java方也定义一个类B,里面的成员变量一样。然后将类A与类B对应起来,在转换时类A便会转换为类B,当然反过来也可以。

    要做到这样的一个类的对应很简单。

    只要求在as方类定义前面加上一条代码[RemoteClass(alias="/*classname*/")]即可。如:

package{
   [Bindable]
   [RemoteClass(alias="com.zhan.PersonInfo")]
   public class PersonInfo{
       public var name:String;
       public var age:int;
       public var gender:Boolean;
   }
}


Java方 (get set 就不写出来了):

package com.zhan{
   public class PersonInfo{
       public String name;
       public int age;
       public boolean gender;
   }
}

这样便将两个类对应了起来,其中有几个点要特别注意:

 

1.两个类对应成员命名要求一致,就是一模一样,这样转换时才知道将哪个成员转换为哪个成员

2.命名首字母要小写,这个其实是一编程规范,有时候会忘了。

  (这个规则上可是吃了亏的,怎么也不知道问题出在哪,查了半天,无意间将命名改为小写,居然就可以了,看来平时编程时还是要规范点)

3.java转换为as时,要求as类提拱默认构造函数(即不带参数),从java转换为as时,其实是构造一个as对象去接收,如果这个as类要求有带参数的构造函数,估计会把lcds搞晕,当然吃亏的还是自已


你可能感兴趣的:(Actionscript与Java数据类型转换)