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

时间:2016-01-15 01:11:59

标签: android web-services ksoap2

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

感谢您的帮助..

2 个答案:

答案 0 :(得分:4)

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

  }

}

答案 1 :(得分:1)

我的解决方案

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