在项目中通过WebService调用服务端方法,服务端采用C#编写,当初公司项目应该是没有考虑到android方面(那时候android还没火起来吧),所以在设计方面可能不是那么规范吧(个人理解)。因为服务端返回的数据要么是基础类型数据,要么就是直接返回一个类,返回一个Datatable,返回一个Dataset,一个类里面还可能包含其他的类。这就直接导致了现在android的这块解析返回数据方面产生巨大的麻烦。
幸好,这个问题已经解决了。下面就将给出其中的一些解析方式。
关于在android中调用WebService的方法可以具体参考这篇文章:http://www.open-open.com/bbs/view/1320111271749?sort=newest
其中下面代码当中有用到FanShe这个类在我前面的文章当中有提到过http://blog.csdn.net/fu222cs98/article/details/21552199 里面的 c.
通过反射将obj类的属性列表一个个赋值以及 f. 通过反射对类进行解析并赋值
一、给出数据解析接口
/** * 数据解析接口 */ 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; } }
Mr.傅:学习笔记
欢迎转载,转载注明出处,谢谢