Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android學習之——(3)項目中的WebService返回數據處理學習

Android學習之——(3)項目中的WebService返回數據處理學習

編輯:關於Android編程

接上一篇http://blog.csdn.net/fu222cs98/article/details/21554339 在項目中通過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的情況下:
/**
 * 數據為List
 * @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 clalist = new ArrayList();
        // 一個個類的屬性賦值,判斷是否為空
        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 list = new ArrayList();
        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 clalist = new ArrayList();
        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 list = new ArrayList();
        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.傅:學習筆記
歡迎轉載,轉載注明出處,謝謝

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved