AMF是Adobe捣鼓出来的一种开源的传输格式,用在多个地方,例如flash与后台传输,red5流媒体之类。在Adobe的BlazeDS服务器其实已经提供了如何解析AMF的代码,但由于耦合度有点高,因此有热心人士从BlazeDS服务器源代码中将解析AMF那一部分抽了出来并放在google的代码库里,网址如下:http://code.google.com/p/amf-serializer/,这个类库在Java环境下使用完全没有问题,但在Android中使用的时候,会报一些类似于PropertyDescriptor没找到或者解析不了的错误。原因在于Android1.6的JavaSDK里貌似没有PropertyDescriptor这个类。PropertyDescriptor是一个用于内省的类,其实里面有一些功能我们完全可以用基本的反射来模拟出来:
PropertyDescriptor
import java.lang.reflect.Method; public class PropertyDescriptor { private String name; private String displayName; private Method readMethod; private Method writeMethod; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDisplayName() { return displayName; } public void setDisplayName(String displayName) { this.displayName = displayName; } public Method getReadMethod() { return readMethod; } public void setReadMethod(Method readMethod) { this.readMethod = readMethod; } public Method getWriteMethod() { return writeMethod; } public void setWriteMethod(Method writeMethod) { this.writeMethod = writeMethod; } }
import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class PropertyDescriptorHelper { public static List<PropertyDescriptor> getPropertyDescriptors(Class<?> type) { List<PropertyDescriptor> lsDescriptor = new ArrayList<PropertyDescriptor>(); Method[] aryMethod = type.getMethods(); Map<String, Method> dicMethod = new HashMap<String, Method>(); for (Method method : aryMethod) { if(method.getName().startsWith("set") && method.getParameterTypes() != null && method.getParameterTypes().length == 1) { dicMethod.put(method.getName(),method); } } for(Method method : aryMethod) { if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { String name = Character.toLowerCase(method.getName().charAt(3)) + method.getName().substring(4, method.getName().length()); PropertyDescriptor desc = new PropertyDescriptor(); desc.setDisplayName(name); desc.setName(name); desc.setReadMethod(method); String setMethodName = "set" + name.substring(0,1).toUpperCase() + name.substring(1, name.length()); if(dicMethod.containsKey(setMethodName)){ desc.setWriteMethod(dicMethod.get(setMethodName)); } lsDescriptor.add(desc); } else if (method.getName().startsWith("is") && method.getParameterTypes().length == 0) { String name = Character.toLowerCase(method.getName().charAt(2)) + method.getName().substring(3, method.getName().length()); PropertyDescriptor desc = new PropertyDescriptor(); desc.setDisplayName(name); desc.setName(name); desc.setReadMethod(method); String setMethodName = "set" + name.substring(0,1).toUpperCase() + name.substring(1, name.length()); if(dicMethod.containsKey(setMethodName)){ desc.setWriteMethod(dicMethod.get(setMethodName)); } lsDescriptor.add(desc); } } return lsDescriptor; } public static List<PropertyDescriptor> getPropertyDescriptors(Object bean) { return getPropertyDescriptors(bean.getClass()); } }
有了这两个类以后,就可以查找amf-serializer类库中所有使用到PropertyDescriptor的地方,替换为我们写的这两个类,这样解析AMF的基本功能就具备了。
不过单纯使用这个类库,只是能解析AMF,但与服务端交互的功能还没有。这个也很简单,BlazeDS服务器源代码里就有一个AMFConnection类,这个类包装了JDK里的HttpURLConnection类进行网络传输并对AMF进行序列化反序列化,我们只需要把这个抽出来修改一下就可以用。
顺便提一下,AMF其实是支持多个方法调用的,原理就是在一个AMFMessage里添加多个AMFBody,每一个AMFBody就是一个方法调用,但AMFConnection默认并不支持多个方法调用,大家参考AMFConnection类里的call方法自行添加即可。
完整的demo请到这里下载:http://download.csdn.net/detail/visualcatsharp/4294746
没资源分的请留下邮箱。