1

您好我正在尝试构建一个从网络服务检索数据的应用程序。我正在使用 KSOAP2 从 Web 服务获取数据。数据从 Web 服务传入 arraylist。进入数组列表..

下面是代码

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

3 回答 3

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

result是一个Object,你正试图投进一个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
            }
于 2012-08-21T15:22:23.580 回答
0

我不知道返回什么envelope.getResponse();,但我看到你将它存储在一个Object.

然后,您尝试将其转换为ArrayList<String>. 那是行不通的,Object 不是 ArrayList。

于 2012-08-21T15:18:21.667 回答
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);
            }
于 2012-08-21T20:47:13.383 回答