quinta-feira, 9 de abril de 2015

Android - Deserialization

public class Deserialization {


    @SuppressWarnings("unchecked")
    public  ArrayList SoapDeserializeArray(Class itemClass,SoapObject object) {


        ArrayList arrayList=new ArrayList();

        for(int i=0;i            try {
                Object newObject=itemClass.getConstructor(SoapObject.class).newInstance(((SoapObject)object.getProperty(i)));
                arrayList.add(newObject);
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return   arrayList;

    }

    public  void SoapDeserialize(T item,SoapObject object){

        Field[] fields=item.getClass().getDeclaredFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            String fieldTypeName = field.getType().getName();
            try {
                if(fieldTypeName==String.class.getName()){

                    String value = object.getProperty(fieldName).toString();
                    int idx = value.lastIndexOf("anyType{}");
                    if( idx>0 || value.equals("anyType{}"))
                        value = "";

                    field.set(item,value);
                }
                else if(fieldTypeName==Integer.class.getName()||fieldTypeName==int.class.getName())
                {
                    field.set(item,Integer.parseInt(object.getProperty(fieldName).toString()));
                }
                else if(fieldTypeName==Float.class.getName())
                {
                    field.set(item,Float.parseFloat(object.getProperty(fieldName).toString()));
                }
                else if(fieldTypeName==Double.class.getName())
                {
                    field.set(item,Double.parseDouble(object.getProperty(fieldName).toString()));
                }
                else if(fieldTypeName==Boolean.class.getName())
                {
                    field.set(item,Boolean.parseBoolean((object.getProperty(fieldName).toString())));
                }
                else if( List.class.isAssignableFrom(field.getType()))
                {
                    SoapObject fieldArray=(SoapObject)(object.getProperty(fieldName));
                    ParameterizedType parameterizedType =(ParameterizedType)field.getGenericType();
                    Class genericClass=(Class)(parameterizedType.getActualTypeArguments()[0]);
                    ArrayList list=new ArrayList();
                    for (int i=0;i                        Object newObject = genericClass.getConstructor(SoapObject.class).newInstance((SoapObject)(fieldArray.getProperty(i)));
                        list.add(newObject);
                    }
                    field.set(item,list);
                }
                else {
                    Class class1=field.getType();
                    Object newObject=class1.getConstructor(SoapObject.class).newInstance((SoapObject)(object.getProperty(field.getName())));
                    field.set(item, newObject);
                }
            } catch (Exception e) {
                Log.e("FieldNotFound:", " "+e.getMessage());
            }
        }
    }
}
Postar um comentário