java.lang.ClassCastException: java.util.Vector 无法转换为 org.ksoap2.serialization.SoapObject

java.lang.ClassCastException: java.util.Vector cannot be cast to org.ksoap2.serialization.SoapObject

我正在尝试 returns 使用 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;
}

感谢您的帮助..

我的解决方案:

Vector<SoapObject> result = null;
    ArrayList<Smartphone> ss = new ArrayList<Smartphone>();
    Smartphone smart;
    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);

        envelope.addMapping(NAMESPACE, "Smartphone", new Smartphone().getClass());
        androidHttpTransport.call(SOAP_ACTION, envelope);
        result = (Vector<SoapObject>) envelope.getResponse();
        int length = result.size();
        for (int i = 0; i < length; ++i) {
            SoapObject so = result.get(i);
            smart = new Smartphone();
            for (int j = 0; j < so.getPropertyCount(); j++) {
                smart.setProperty(j, so.getProperty(j));
            }
            ss.add(smart);
        }

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

在我的例子中,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);

  }

}