您好我正在尝试构建一个从网络服务检索数据的应用程序。我正在使用 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)