3

我正在尝试从用 PHP(nuSOAP)编写的肥皂服务接收复杂对象的数组。我正在尝试编写一个 Android 客户端并使用 ksoap2 库(3.0.0 RC.4)。这个网上有一些“解决方案”,有几个人面临同样的问题——无论如何,我尝试了很多不同的方法,但我现在仍然卡住了好几天,所以我决定向你们寻求帮助。因此,我将向您展示我认为最接近我想要获得的代码。

首先要做的事情 - SOAP-Response(主体):

<SOAP-ENV:Body>
    <ns1:GetListResponse xmlns:ns1="http://localhost/games_db/games_db.php">
        <return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:DataPlusID[24]">
            <item xsi:type="tns:DataPlusID">
                <data xsi:type="xsd:string">shitload</data>
                <ID xsi:type="xsd:int">4</ID>
            </item>
            <item xsi:type="tns:DataPlusID">
                <data xsi:type="xsd:string">of</data>
                <ID xsi:type="xsd:int">7</ID>
            </item>
            <item xsi:type="tns:DataPlusID">
                <data xsi:type="xsd:string">imformation</data>
                <ID xsi:type="xsd:int">10</ID>
            </item>
        </return>
    </ns1:GetListResponse>
</SOAP-ENV:Body>

当我没有映射任何东西时,“envelope.bodyIn.toString()”给了我以下信息。

GetListResponse{
    return=[
        DataPlusID{data=shitload; ID=4; },
        DataPlusID{data=of; ID=7; },
        DataPlusID{data=information; ID=10; }
    ];
}

这是课程,有一天应该会处理响应...

public class GetListResponse implements KvmSerializable {

    private Vector<DataPlusID> datavector = new Vector<DataPlusID>();

    @Override
    public Object getProperty(int arg0) {
        return this.datavector;
    }

    @Override
    public int getPropertyCount() {
        return 1;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
        info.name = "return";
        info.type = new Vector<DataPlusID>().getClass();
    }

    @Override
    public void setProperty(int index, Object value) {
        this.datavector = (Vector<DataPlusID>) value;
    }
}

public class DataPlusID implements KvmSerializable
{

    private String data;
    private int ID;


    @Override
    public Object getProperty(int arg0) {
        switch(arg0) {
        case 0:
            return data;
        case 1:
            return ID;
        }

        return null;
    }

    @Override
    public int getPropertyCount() {
        return 2;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch(index) {
        case 0:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "data";
            break;
        case 1:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "ID";
            break;
        default:break;
        }
    }

    @Override
    public void setProperty(int index, Object value) {
        switch(index) {
        case 0:
            data = value.toString();
            break;
        case 1:
            ID = Integer.parseInt(value.toString());
            break;
        default:
            break;
        }
    }
}

这是接收消息的代码

public GetListResponse GetList(String liste) throws Exception{

    SoapObject request = new SoapObject(Namespace, MethodGetList);

    request.addProperty("list", liste);

    final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);

    envelope.addMapping(Namespace, "GetListResponse", new GetListResponse().getClass());
    //envelope.addMapping(Namespace, "return", new Vector<DataPlusID>().getClass());
    envelope.addMapping(Namespace, "item", new DataPlusID().getClass()); //tried also "DataPlusID" instead of "item"

    try {
        HttpTransportSE transport = new HttpTransportSE(URL);
        transport.debug = true;

        transport.call(ActionGetList, envelope);

        Log.d("SOAPEnvelope", "Response: "+transport.responseDump);
    } catch (Exception e){
        Log.d("SOAPEnvelope", "Fehler bei Serverabfrage: "+e.toString());
    }
    //Log.d("SOAPEnvelope", "BodyIn: "+envelope.bodyIn.toString());

    GetListResponse result = new GetListResponse();
    result = (GetListResponse)envelope.bodyIn;

    return result;
}

处理数据:

new Thread(new Runnable() {
    public void run() {  
        try {
            GetListResponse response = new GetListResponse();
            response = GetList("genre");

            //content of datavector is below
            Vector<DataPlusID> datavector = new Vector<DataPlusID>();
            datavector = (Vector<DataPlusID>) response.getProperty(0);

            //EXCEPTION IS THROWN HERE
            String x = (String) datavector.get(0).getProperty(0);

            DataPlusID daten0 = new DataPlusID();
            //WOULD ALSO HAPPEN HERE
            daten0 = (DataPlusID) datavector.get(0);

            String genre1 = (String) daten0.getProperty(0);

        } catch (Exception e) {
            Log.d("SOAPEnvelope", e.toString());
        }
    }
}).start();

这是“数据向量”的内容:

(java.util.Vector)
[DataPlusID{data=Action; ID=4; },
DataPlusID{data=Adventure; ID=7; },
DataPlusID{data=Aufbauspiel; ID=10; },
DataPlusID{data=Beat 'em up; ID=11; }]

抛出以下异常:

java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to com.example.gamesdb_client.DataPlusID

最奇怪的是,在调试时我实际上得到了我想要得到的值。--> 当我检查表达式“datavector.get(0).getProperty(0)”时,我得到:

(java.lang.String) shitload

所以他实际上是以正确的格式(字符串)查看数据,但是当他尝试将其附加到字符串变量时,他给了我一个 ClassCastException?

无论如何,无论我尝试什么,最终结果都是 CCE,所以我很确定问题可以在映射(以及类定义)部分中找到:

1 envelope.addMapping(Namespace, "GetListResponse", new GetListResponse().getClass());
2 envelope.addMapping(Namespace, "item", new DataPlusID().getClass());

如果没有第 1 行,我会得到另一个 CCE,所以我希望映射能够按预期工作。如果没有第 2 行,没有任何变化,所以我很确定问题出在 DataPlusID 类中。

他只是无法链接这些:

DataPlusID{data=shitload; ID=4; },
DataPlusID{data=of; ID=7; },
DataPlusID{data=information; ID=10; }

到 DataPlusID 类。

所以希望有人可以看看这个并给我一些如何解决问题的想法。也许只是一些我不明白的基本东西。- 让我的假期不被浪费^^谢谢。

编辑:重新 R4j

问题是,在示例代码中,他们使用了一个字符串向量(用 this.add(value.toString() 添加它)。这对我来说是不可能的,因为我有一个“DataPlusID”向量。有些东西like this.add(value); 不起作用,因为他不能将参数(类型:对象)放入 DataPlusID 向量中。

我试图通过将“GetListResponse”类的 setProperty 方法更改为:

public void setProperty(int index, Object value) {
    this.datavector = (Vector<DataPlusID>) value;

    for (int i = 0; i < datavector.size(); i++) {
        this.add(datavector.elementAt(i));
    }
}

这样我就可以将向量的单个部分设置到类中。

不幸的是,这只是在该行中抛出了相同的 ClassCastExcepiton:

this.add(datavector.elementAt(i));
4

2 回答 2

6

原始 postet 问题的解决方案。

归功于 R4j - 你的回答对我帮助很大。

类定义 - GetListResponse:

public class GetListResponse implements KvmSerializable {

    private Vector<SoapObject> datavector = new Vector<SoapObject>();
    private DataArray dataarray = new DataArray();

    @Override
    public Object getProperty(int arg0) {
        //return this.datavector.get(arg0);
        return this.dataarray;
    }

    @Override
    public int getPropertyCount() {
        return 1;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
        info.name = "return";
        info.type = new Vector<DataPlusID>().getClass();
    }

    @Override
    public void setProperty(int index, Object value) {
        this.datavector = (Vector<SoapObject>) value;

        for(int i = 0; i < datavector.size(); i++) {
            dataarray.setProperty(0, datavector.get(i));
        }
    }
}

数据数组:

public class DataArray extends Vector<DataPlusID> implements KvmSerializable {

    private static final long serialVersionUID = -1166006770093411055L;

    @Override
    public Object getProperty(int index) {
        return this.get(index);
    }    

    @Override
    public int getPropertyCount() {
        return this.size();
        //return dataArray.length;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
        info.name = "item";
        info.type = new DataPlusID().getClass();
    }

    @Override
    public void setProperty(int index, Object value) {
        SoapObject soapObject = new SoapObject();
        soapObject = (SoapObject) value;

        DataPlusID daten = new DataPlusID();
        daten.setProperty(0, soapObject.getProperty("data"));
        daten.setProperty(1, soapObject.getProperty("ID"));

        this.add(daten);
    }
}

数据加号:

public class DataPlusID implements KvmSerializable
{

    private String data;
    private int ID;

    @Override
    public Object getProperty(int arg0) {

        switch(arg0)
        {
        case 0:
            return data;
        case 1:
            return ID;
        }        
        return null;
    }

    @Override
    public int getPropertyCount() {
        return 2;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch(index)
        {
        case 0:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "data";
            break;
        case 1:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "ID";
            break;
        default:break;
        }
    }

    @Override
    public void setProperty(int index, Object value) {
        switch(index)
        {
        case 0:
            data = value.toString();
            break;
        case 1:
            ID = Integer.parseInt(value.toString());
            break;
        default:
            break;
        }
    }
}

接收数据的代码:

public GetListResponse GetList(String liste) throws Exception{

SoapObject request = new SoapObject(Namespace, MethodGetList);

request.addProperty("list", liste);

    final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);

    //this is the only necessary mapping
    envelope.addMapping(Namespace, "GetListResponse", new GetListResponse().getClass());

    try {
        HttpTransportSE transport = new HttpTransportSE(URL);
        transport.debug = true;

        transport.call(ActionGetList, envelope);

    } catch (Exception e){
        Log.d("SOAPEnvelope", "Fehler bei Serverabfrage: "+e.toString());
    }
    GetListResponse result = new GetListResponse();
    result = (GetListResponse) envelope.bodyIn;

    return result;
}

处理数据:

new Thread(new Runnable() {
    public void run() {  
        try {
            @SuppressWarnings("unchecked")
            GetListResponse response = GetList("genre");

            DataArray array = (DataArray) response.getProperty(0);

            DataPlusID daten = (DataPlusID) array.getProperty(2);

            String x = (String) daten.getProperty(0);

            Log.d("SOAPEnvelope", "element 3 = "+x);
            //Log says "element 3 = information"
        } catch (Exception e) {
            Log.d("SOAPEnvelope", e.toString());
        }
    }
}).start();
于 2013-01-11T20:58:03.953 回答
5

据我所知,您的 GetListResponse 类应该扩展 Vector 而不是包含它。在此处检查文档

 public class GetListResponse extends Vector<DataPlusID> 
                                implements KvmSerializable {
  }

更新
对不起,我以前从未尝试过对复杂对象数组使用映射方法。您可以尝试另一种方法,通过使用构建循环迭代Vector<SoapObject>并手动获取其属性(您不需要GetListResponse类)。像这样的东西:

Vector<SoapObject> vectorOfSoapObject = (Vector<SoapObject>)envelop.getResponse();
for (SoapObject soapObject : vectorOfSoapObject) {
   // put all properties into  DataPlusID  object
   DataPlusID  dataPlusIDObj = new DataPlusID();
   dataPlusIDObj.setData(soapObject.getPropertyAsString("data"));
}

这种方法在文档中,它应该可以工作。

于 2013-01-09T03:15:27.997 回答