如何将ArrayList作为SoapObject返回

时间:2013-03-05 09:11:40

标签: android arraylist ksoap

我刚接触Android并且在过去的几天里坚持了一些东西。 Iam从数据库中检索CandidateNames并将其存储到ArrayList中的WebService并将ArrayList返回给Java客户端。

WebServiceArrayList

public  ArrayList<String> DisplayName(){
    ArrayList<String>  results= new ArrayList<String>(); 
    try {
        String connectionURL = "jdbc:mysql://localhost/databaseName";
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection=DriverManager.getConnection(connectionURL,"username","pswd");
        statement = connection.createStatement();

        String QueryString = "Select CName From CMaster where cid='xyz'";
        rs = statement.executeQuery(QueryString);

        while (rs.next()) {
            results.add(rs.getString(1));
        }

        rs.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return results;
}

Java代码:

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);            
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true; 
envelope.setOutputSoapObject(request);

try {
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive response = (SoapPrimitive)envelope.getResponse();    
    System.out.println("Response:::::::::::::" +result);
}

这只给了我ArrayList的第一个值。 当我用

替换SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

SoapObject result= (SoapObject)envelope.getResponse(); System.out.println("Response:::::::::::::" +result.getProperty(0)); 它抛出以下exception

java.lang.classcastexception org.ksoap2.serialization.soapprimitive cannot be cast to org.ksoap2.serialization.soapObject

我用Google搜索并尝试了所有可能的事情,如果我错过了什么,请帮助我。 提前谢谢!

2 个答案:

答案 0 :(得分:0)

这对我有用。希望能帮助到你。我还有一个WS正在返回一个Arraylist

SoapObject result = (SoapObject) envelope.bodyIn;
int count = result.getPropertyCount();
ArrayList<String> simplifiedList = new ArrayList<String>(); 
for (int i = 0; i < count; i++)
{
    simplifiedList.add(result.getPropertyAsString(i));
}

所以在这一点上你有一个Arraylist,simplifiedList,你可以将它传递给另一个活动或用它做任何其他事情。

此致

答案 1 :(得分:0)

我遇到了同样的问题,我使用的是一个返回ArrayList的WS,而我的Object_A有自己的ArrayList,对于Object_B来说,它们是自己的ArrayList,无论我用这个解决了什么:

SoapObject myArray_A=null;

SoapObject myArray_B=null;

SoapObject myArray_C=null;

myArray_A=envelop.bodyIn;
for(int x=0; x<myArray_A.getPropertyCount()-1;x++){

myArray_B = (SoapObject) myArray_A.getProperty(x);
for(int y=0; y<myArray_B.getPropertyCount()-1;y++){
    myArray_C = (SoapObject) myArray_B.getProperty(y);
    for(int z=0; z<myArray_C.getPropertyCount()-1;z++){
        SoapPrimitive porperty = (SoapPrimitive) myArray_B.getProperty(z);
    }
}}