1

Iam new to Android and stuck up with something for past couple of days. Iam retreiving CandidateNames from database and storing it to my ArrayList which is in a WebService and returning the ArrayList to Java Client.

WebService with ArrayList:

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 Code:

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

This gives me only the first value of my ArrayList. When I replace: SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); with

SoapObject result= (SoapObject)envelope.getResponse(); System.out.println("Response:::::::::::::" +result.getProperty(0)); it throws the following exception:

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

I Googled and tried every possible thing,please help me out if i missed out something. Thanks in advance!

4

2 回答 2

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);
    }
}}
于 2013-08-27T18:33:23.570 回答
0

这对我有用。希望能帮助到你。我也有一个 WS whihc 返回一个 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,你可以将它传递给另一个活动或用它做任何其他事情。

问候,

于 2013-06-01T17:49:01.480 回答