我试图用ksoap返回一个对象数组
我在这一行出错了:
result = (SoapObject) envelope.getResponse();
这是我的代码:
public static List<Smartphone> GetAllSmart() {
SoapObject result=null;
try {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = false;
envelope.setOutputSoapObject(request);
//envelope.encodingStyle = SoapSerializationEnvelope.XSD;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
result = (SoapObject) envelope.getResponse();
} catch (IOException e) {
Log.d("Error", "Some exception occurred", e);
} catch (XmlPullParserException e) {
Log.d("Error", "Some exception occurred", e);
} catch (NetworkOnMainThreadException e) {
Log.d("Error", "Some exception occurred", e);
}
return RetrieveFromSoap(result);
}
public static List<Smartphone> RetrieveFromSoap(SoapObject soap) {
ArrayList<Smartphone> ss = new ArrayList<Smartphone>();
for (int i = 0; i < soap.getPropertyCount(); i++) {
Smartphone smart = new Smartphone();
int j=0;
for(j=0; j<6;j++){
smart.setProperty(j, soap.getProperty(i));
}
ss.add(smart);
}
return ss;
}
谢谢你的帮助。
最佳答案
在我的例子中,web服务的响应因列表和单个对象而异。所以我两个都用了
Vector<SoapObject> result = (Vector<SoapObject>) envelope.getResponse();
和
SoapObject response = (SoapObject) envelope.getResponse();
所以使用下面的代码来处理这两个场景。
try {
httpTransport.call(esbRequest.getAction(), envelope, headerList);
result = (Vector<SoapObject>) envelope.getResponse();
} catch (SoapFault soapFault) {
LOGGER.error("Soap Fault Exception " + soapFault);
} catch (IOException ioException) {
LOGGER.error("IOException " + ioException);
} catch (XmlPullParserException parserException) {
LOGGER.error("XmlPullParserException " + parserException);
} catch (Exception exception) {
LOGGER.error("Exception " + exception);
try {
response = (SoapObject) envelope.getResponse();
} catch (SoapFault e) {
// TODO Auto-generated catch block
LOGGER.error("Soap Fault Exception " + e);
}
}
关于android - java.lang.ClassCastException:无法将java.util.Vector强制转换为org.ksoap2.serialization.SoapObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34802771/