java.lang.ClassCastException:java.util.Vector无法转换为org.ksoap2.serialization.SoapObject [英] java.lang.ClassCastException: java.util.Vector cannot be cast to org.ksoap2.serialization.SoapObject

查看:339
本文介绍了java.lang.ClassCastException:java.util.Vector无法转换为org.ksoap2.serialization.SoapObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用KSOAP返回对象数组 我在这行中得到错误:

i'm trying to returns an array of objects with KSOAP i get error in this line:

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

这篇关于java.lang.ClassCastException:java.util.Vector无法转换为org.ksoap2.serialization.SoapObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆