Pregunta

Hi I am trying to build an app where data are retrieved from a webs service. I am using KSOAP2 to get the data from the web service.. the data is getting passed in arraylist from the web service.. I need to save the data received in a new arraylist but I am getting ClassCastException when I try to save the object into array list..

below is the code

class NetworkConnectTask extends AsyncTask<String, Void, ViewSchedule> {

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }

    protected ViewSchedule doInBackground(String... urls) {
        try
        {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("username",username);
            request.addProperty("startdate",currentDateString);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION,envelope);
            Object result = envelope.getResponse();

            ArrayList<String> recivedlistItems = new ArrayList<String>();
            recivedlistItems=(ArrayList<String>) result; // exception caught here


            if(!recivedlistItems.isEmpty())
            {

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        addItems("some crap");

                    }
                });
            }
            else
            {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        addItems("No bookings");

                    }
                });
            }
        }
        catch (Exception e)
        {
            System.out.println("error");
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(LoginScreen feed) {



    }

Here is code of my wsdl (web-service)

public synchronized ArrayList<String> View(String username, String startdate ) {

    System.out.println("\n entered the method");

    try {
        /* Create object of the Connect class */

        Connect connect = new Connect();
        /*
         * calling the Connection method of Connect class to establish a
         * database connection
         */
        Connection conn = connect.Connection();

        java.sql.PreparedStatement preparedStatement = null;
        /* Creating a query to be executed with prepared statement */
        String query = "select title,startdate,enddate from bookinginformation where username=? and startdate=?";

        preparedStatement = conn.prepareStatement(query);
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, startdate);
        // stmt.executeUpdate(query);

        ResultSet rs = preparedStatement.executeQuery();

        boolean empty = true;
        while (rs.next()) {
            empty = false;
            String title =rs.getString(1);
            String S_date=rs.getString(2);
            String e_date=rs.getString(2);
            StringBuilder sb=new StringBuilder();
            String temp="";
            temp = sb.append(title).append("-").append(S_date).append("-")
                    .append(e_date).toString();
            listItems.add(temp);
            System.err.println("\n added  \n");
        }

        if (empty) {
            System.out.println("\n no data");
            //return "false";
        }
        rs.close();
        conn.close();

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Cannot connect to database server");
        System.exit(0);
    }
    return listItems;


}

Below is the error

java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive
at com.example.androidfrontend.ViewSchedule$NetworkConnectTask.doInBackground(ViewSchedule.java:162)
at com.example.androidfrontend.ViewSchedule$NetworkConnectTask.doInBackground(ViewSchedule.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
¿Fue útil?

Solución 3

Slight change to Swayam's code worked for me..

SoapObject result=(SoapObject)envelope.bodyIn;
            int childCount = result.getPropertyCount();

            int i;
            for (i = 0; i < childCount; i++) {
                String temp= result.getProperty(i).toString();
                recivedlistItems.add(temp);
            }

Otros consejos

recivedlistItems=(ArrayList<String>) result; // wrong code

result is an Object and you are trying to cast into a String. And hence the exception.

Try this code.

            SoapObject result = (SoapObject) envelope.getResponse(); 
            int childCount = result.getPropertyCount();

            int i;
            for (i = 0; i < childCount; i++) {
                recivedlistItems.add(result.getProperty(i).toString()); //**edited** after Srikanth pointed it out
            }

I don't know what returns envelope.getResponse();, but I see you store it in an Object.

Then you are trying to cast it to an ArrayList<String>. That won't work, an Object is not an ArrayList.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top