Search code examples
androidweb-servicesksoap2

Android retrieve list object from Webservice by KSOAP


The first, I just created the dynamic web project for Webservice (Axis2), return the list object

@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;
    }
}

The second, I have created a Android Application Project to call deployed Webservice by KSOAP. But I don't know how to retrieve object and get items from it. It seem to be harder than I retrieve list string(ArrrayList - I did it easily)

Anyone can help me resolve this case. Thanks a lot!


Solution

  • You should parse your response manually.

    // ...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;