/** * 数据解析接口 */ public interface ParseData { /** * @param result * Soap数据对象 * @param cla * Class类 * @param entityname * 包名 * @return */ public abstract Object doing(SoapObject result, @SuppressWarnings("rawtypes") Class cla, String entityname) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException; }二、返回数据为单个类的情况
/** * 数据为单个类 * @author Administrator */ public class OneClass implements ParseData { @Override public Object doing(SoapObject result, @SuppressWarnings("rawtypes") Class cla, String entityname) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException { // TODO Auto-generated method stub FanShe fs = new FanShe(); Object obj = fs.useFanSheToData(result, cla.newInstance(), entityname); return obj; } }三、数据为多个String的情况
/** * 数据为多个String * @author Administrator */ public class ManyString implements ParseData { @Override public Object doing(SoapObject result, @SuppressWarnings("rawtypes") Class cla, String entityname) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException { // TODO Auto-generated method stub String[] zsts = new String[result.getPropertyCount()]; for (int i = 0; i < result.getPropertyCount(); i++) { zsts[i] = result.getProperty(i).toString(); } return zsts; } }四、数据为List<Object>的情况下:
/** * 数据为List<Object> * @author Administrator syc */ public class GetListClass implements ParseData { @Override public Object doing(SoapObject result, @SuppressWarnings("rawtypes") Class cla, String entityname) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException { // TODO Auto-generated method stub boolean flag = false; FanShe fs = new FanShe(); List<Object> clalist = new ArrayList<Object>(); // 一个个类的属性赋值,判断是否为空 for (int j = 0; j < result.getPropertyCount(); j++) { if (((SoapObject) result.getProperty(j)).getProperty(0).toString() .indexOf("anyType") >= 0) { flag = true; break; } } //不为空反射 if (flag) { SoapObject table = (SoapObject) result.getProperty(0); for (int j = 0; j < table.getPropertyCount(); j++) { if (table.getProperty(j).toString().indexOf("anyType") >= 0) { // System.out.println(table.getProperty(j).toString()); Object obj = fs.useFanSheToData( (SoapObject) table.getProperty(j), cla.newInstance(), entityname); clalist.add(obj); } } } else { for (int i = 0; i < result.getPropertyCount(); i++) { SoapObject table = (SoapObject) result.getProperty(i); Object obj = fs.useFanSheToData(table, cla.newInstance(), entityname); clalist.add(obj); } } return clalist; } }五、数据为DataTable的情况下解析
/** * 数据为DateTable类型解析 * @author Administrator syc */ public class DataTable implements ParseData { @Override public Object doing(SoapObject result, @SuppressWarnings("rawtypes") Class cla, String entityname) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException { // TODO Auto-generated method stub FanShe fs = new FanShe(); List<Object> list = new ArrayList<Object>(); if (result.toString().indexOf("diffgram") >= 0) { //System.out.println(result.getProperty("diffgram").toString()); if (result.getProperty("diffgram").toString().equals("anyType{}")) { return null; } SoapObject diffgram = (SoapObject) result.getProperty("diffgram"); SoapObject documentelement = (SoapObject) diffgram.getProperty(0); for (int i = 0; i < documentelement.getPropertyCount(); i++) { // System.out.println(i); // System.out.println(documentelement.getPropertyCount()); SoapObject bedtable = (SoapObject) documentelement .getProperty(i); Object obj = fs.useFanSheToData(bedtable, cla.newInstance(), entityname); list.add(obj); } } return list; } }六、数据为DataSet的数据解析
/** * 数据为DateSet类型解析 * @author Administrator */ public class DataSet implements ParseData { @Override public Object doing(SoapObject result, @SuppressWarnings("rawtypes") Class cla, String entityname) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException { // TODO Auto-generated method stub Object obj = null; if (result.toString().indexOf("diffgram") >= 0) { SoapObject diffgram = (SoapObject) result.getProperty("diffgram"); obj = obtainshuju(diffgram, cla, entityname); } return obj; } /** * 有diffgram把传回来的list数据解析在反射 * @param diffgram * @param cla */ public Object obtainshuju(SoapObject diffgram, @SuppressWarnings("rawtypes") Class cla, String entityname) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException { FanShe fs = new FanShe(); List<Object> clalist = new ArrayList<Object>(); SoapObject NewDataSet = (SoapObject) diffgram.getProperty("NewDataSet"); if (NewDataSet.getPropertyCount() == 1 && ((SoapObject) NewDataSet.getProperty(0)).getPropertyCount() == 1) { return ((SoapObject) NewDataSet.getProperty(0)).getProperty(0) .toString(); } // 取得一个个类的属性赋值 for (int i = 0; i < NewDataSet.getPropertyCount(); i++) { SoapObject table = (SoapObject) NewDataSet.getProperty(i); System.out.println(table.toString()); Object obj = fs.useFanSheToData(table, cla.newInstance(), entityname); clalist.add(obj); } return clalist; } }七、类中有类的数据解析
/** * 类中有类 * @author Administrator syc */ public class Classinclass implements ParseData { @Override public Object doing(SoapObject result, @SuppressWarnings("rawtypes") Class cla, String entityname) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException { FanShe fs = new FanShe(); List<Object> list = new ArrayList<Object>(); for (int i = 0; i < result.getPropertyCount(); i++) { SoapObject result1 = (SoapObject) result.getProperty(i); // System.out.println(result.toString()); Object obj = fs.useFanSheToData(result1, cla.newInstance(), entityname); list.add(obj); } return list; } }
Android学习之——项目中的WebService返回数据处理学习,布布扣,bubuko.com
Android学习之——项目中的WebService返回数据处理学习
原文:http://blog.csdn.net/fu222cs98/article/details/21556651