Ksoap2 ClassCastException

时间:2012-08-21 15:15:45

标签: android ksoap2 android-ksoap2 ksoap

您好我正在尝试构建一个应用程序,从Web服务中检索数据。我正在使用KSOAP2从Web服务获取数据。 数据从web服务传递到arraylist中。我需要保存在新的arraylist中收到的数据,但是当我尝试将对象保存到数组列表中时,我得到了ClassCastException。

下面是代码

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) {



    }

这是我的wsdl(网络服务)

的代码
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;


}

以下是错误

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)

3 个答案:

答案 0 :(得分:1)

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

resultObject,您正试图加入String。因此例外。

试试这段代码。

            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
            }

答案 1 :(得分:0)

我不知道返回envelope.getResponse();的内容,但我看到您将其存储在Object中。

然后您尝试将其投射到ArrayList<String>。这不起作用,Object不是ArrayList。

答案 2 :(得分:0)

对Swayam代码的轻微更改对我有用..

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