gpt4 book ai didi

java - ksoap2 通过 SOAP 接收复杂对象数组

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:32 25 4
gpt4 key购买 nike

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

首先要做的事 - SOAP 响应(正文):

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

这里是“datavector”的内容:

(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

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

我试图通过将“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));
}
}

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

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

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

最佳答案

原始 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();

关于java - ksoap2 通过 SOAP 接收复杂对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14186279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com