Android通过KSOAP从Webservice检索列表对象

时间:2013-05-27 18:05:10

标签: android web-services ksoap2

首先,我刚刚为Webservice(Axis2)创建了动态Web项目,返回列表对象

@ MedicineDTO.java

    package com.med.dic.dto;

public class MedicinesDTO {
    private String name;
    private String utility;
    private String manufacture;
    private String ingredient;
    private String indication;
    private String contraindication;
    private String typeOfPackage;
    private String warning;
    private String dosingAndUse;
    private String storage;

    public MedicinesDTO(String name,String utility, String manufacture, String ingredient,
                    String indication, String contraindication, String typeOfPackage,
                    String warning, String dosingAndUse, String storage) {
        this.name = name;
        this.utility = utility;
        this.manufacture = manufacture;
        this.ingredient = ingredient;
        this.indication= indication;
        this.contraindication = contraindication;
        this.typeOfPackage = typeOfPackage;
        this.warning = warning;
        this.dosingAndUse = dosingAndUse;
        this.storage = storage;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUtility() {
        return utility;
    }

    public void setUtility(String utility) {
        this.utility = utility;
    }

    public String getManufacture() {
        return manufacture;
    }

    public void setManufacture(String manufacture) {
        this.manufacture = manufacture;
    }

    public String getIngredient() {
        return ingredient;
    }

    public void setIngredient(String ingredient) {
        this.ingredient = ingredient;
    }

    public String getIndication() {
        return indication;
    }

    public void setIndication(String indication) {
        this.indication = indication;
    }

    public String getContraindication() {
        return contraindication;
    }

    public void setContraindication(String contraindication) {
        this.contraindication = contraindication;
    }

    public String getTypeOfPackage() {
        return typeOfPackage;
    }

    public void setTypeOfPackage(String typeOfPackage) {
        this.typeOfPackage = typeOfPackage;
    }

    public String getWarning() {
        return warning;
    }

    public void setWarning(String warning) {
        this.warning = warning;
    }

    public String getDosingAndUse() {
        return dosingAndUse;
    }

    public void setDosingAndUse(String dosingAndUse) {
        this.dosingAndUse = dosingAndUse;
    }

    public String getStorage() {
        return storage;
    }

    public void setStorage(String storage) {
        this.storage = storage;
    }

}

@ MedicineJDBCFinder.java

package com.med.dic.finder;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.med.dic.dto.MedicinesDTO;
import com.sun.corba.se.impl.util.Version;

public class MedicineJDBCFinder {

    public ArrayList<MedicinesDTO> loadDatabase(String txtSearch) throws ClassNotFoundException {
        ArrayList<MedicinesDTO> myList = new ArrayList<>();
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://127.0.0.1:3306/smd";
        String user = "root";
        String password = "passw0rd";

        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM medicine);
            while (rs.next()) {
                String name = rs.getString("NAME");
                String utility = rs.getString("UTILITY");
                String manufacture = rs.getString("MANUFACTURE");
                String ingredient = rs.getString("INGREDIENTS");
                String indication = rs.getString("INDICATION");
                String contraindication = rs.getString("CONTRAINDICATION");
                String typeOfPackage = rs.getString("TYPE_OF_PACKAGE");
                String warning = rs.getString("WARNING");
                String dosingAndUse = rs.getString("DOSING_AND_USE");
                String storage = rs.getString("STORAGE");
                myList.add(new MedicinesDTO(name, utility, manufacture, ingredient, indication, contraindication, typeOfPackage, warning, dosingAndUse, storage));
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(Version.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
        return myList;
    }
}

第二,我创建了一个Android应用程序项目来调用KSOAP部署的Webservice。但我不知道如何检索对象并从中获取项目。它似乎比我检索列表字符串更困难(ArrrayList - 我很容易做到)

任何人都可以帮我解决此案。非常感谢!

1 个答案:

答案 0 :(得分:0)

您应该手动解析您的回复。

// ...bla bla bla
        List<MedicinesDTO> list = new ArrayList<MedicinesDTO>();
        SoapObject response = (SoapObject) envelope.getResponse();
        SoapObject soapMedicinesDTOList = (SoapObject) response.getProperty("MedicinesDTOList");
        int itemCount = soapMedicinesDTOList.getPropertyCount();
        for (int i = 0; i < itemCount; i++) {
            MedicinesDTO item = new MedicinesDTO();
            SoapObject soapMedicinesDTO = (SoapObject) soapMedicinesDTOList.getProperty(i);

            if (soapMedicinesDTO.hasProperty("name")) {
                item.setName(soapMedicinesDTO.getPropertyAsString("name"));
            }

            if (soapMedicinesDTO.hasProperty("utility")) {
                item.setUtility(soapMedicinesDTO.getPropertyAsString("utility"));
            }
            // bla bla bla

            list.add(item);
        }

        return list;