如何使用Ksoap 2 </object>将List <object>从Web服务(.NET)方法获取到app android

时间:2012-04-28 02:11:38

标签: web-services ksoap2

假设在视觉工作室我有一个方法:

    [WebMethod]
    public List<PhongTro> GetAllLodgingHousesByAddress(string address)
    {
        return db.GetAllLodgingHousesByAddress(address);
    }

如何将返回数据类型转换为android中的ArrayList?

1 个答案:

答案 0 :(得分:3)

您可以使用以下代码模板:

public ArrayList<PhongTro> getPhongTros() {
        ArrayList<PhongTro> arrPhongTro = new ArrayList<PhongTro>();

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

            /** contains all arrPhongTro objects */
            SoapObject response = (SoapObject) envelope.getResponse();

            /** lists property count */
            final int intPropertyCount = response.getPropertyCount();

            /** loop */
            for (int i = 0; i < intPropertyCount; i++) {
                /** temp SoapObject */
                SoapObject responseChild = (SoapObject) response.getProperty(i);

                /** temp PhongTro object */
                PhongTro tempObj = new PhongTro();

                if (responseChild.hasProperty("att0")) {
                    tempObj.setAtt0(responseChild.getPropertyAsString("att0"));
                }
                if (responseChild.hasProperty("att1")) {
                    tempObj.setAtt1(responseChild.getPropertyAsString("att1"));
                }

                if (responseChild.hasProperty("att2")) {
                    tempObj.setAtt2(responseChild.getPropertyAsString("att2"));
                }

                if (responseChild.hasProperty("att3")) {
                    tempObj.setAtt3(responseChild.getPropertyAsString("att3"));
                }

                if (responseChild.hasProperty("att4")) {
                    tempObj.setAtt4(responseChild.getPropertyAsString("att4"));
                }

                /** Adding temp PhongTro object to list */
                arrPhongTro.add(tempObj);
            }

        } catch (Exception e) {
            e.printStackTrace();

            /** if an error handled arrPhongTro setting null */
            arrPhongTro = null;
        }

        /** returning list */
        return arrPhongTro;
    }