gpt4 book ai didi

android - KSOAP2 处理复杂响应(矢量)

转载 作者:搜寻专家 更新时间:2023-11-01 08:04:24 28 4
gpt4 key购买 nike

三天来,我一直在努力从我的响应中获取复杂类型(列表),但总是收到 ClassCastException

D/SOAPEnvelope(1552): Error: java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to com.example.webservice.ResponsiblepartyGetResponse

我经常阅读几个链接页面/教程,以至于我几乎可以记住它们,包括这四个非常有用的页面:

还有很多,但我还是不明白:(

我希望我能为您提供所有您需要的信息,让我知道我的错误,并给我一个修复它的提示。 Web 服务已经在富客户端应用程序中成功实现,我现在必须使用相同的 Web 服务构建一个 Android 应用程序。

我的第一步是处理“responsibleparty_get”响应。

我的回复看起来像这样(限制为 3,但可以更多/更少)。注意:对于这些响应,“responsiblePartyFacades”还有两个额外的属性为 null(请参阅下面的 JAVADOC)。

responsibleparty_getResponse{
responsiblePartyFacades=anyType{
id=1;
name=hans;
discriminator=person;
admin=false;
};
responsiblePartyFacades=anyType{
id=2;
name=dieter;
discriminator=person;
admin=false;
};
responsiblePartyFacades=anyType{
id=3;
name=stefan;
discriminator=person;
admin=false;
};

相关的 XML 看起来像这样(从 SoapUI 中获取):

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:responsibleparty_getResponse xmlns:ns2="http://ws.my-url.com/">
<responsiblePartyFacades>
<id>1</id>
<name>hans</name>
<discriminator>person</discriminator>
<admin>false</admin>
</responsiblePartyFacades>
<responsiblePartyFacades>
<id>2</id>
<name>dieter</name>
<discriminator>person</discriminator>
<admin>false</admin>
</responsiblePartyFacades>
<responsiblePartyFacades>
<id>3</id>
<name>stefan</name>
<discriminator>person</discriminator>
<admin>false</admin>
</responsiblePartyFacades>
</ns2:responsibleparty_getResponse>
</S:Body>
</S:Envelope>

从我们的 JAVADOC(富客户端)来看,responsiblePartyFacades 看起来是一样的:

 * &lt;complexType name="responsiblePartyFacade">
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}int" minOccurs="0"/>
* &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* &lt;element name="discriminator" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* &lt;element name="admin" type="{http://www.w3.org/2001/XMLSchema}boolean" minOccurs="0"/>
* &lt;element name="children" type="{http://ws.my-url.com/}pairIntBoolFacade" maxOccurs="unbounded" minOccurs="0"/>
* &lt;element name="plantcomponents" type="{http://www.w3.org/2001/XMLSchema}int" maxOccurs="unbounded" minOccurs="0"/>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>

到目前为止,一切都很好。

我编写了这些类来处理响应:

责任方获取响应

public class ResponsiblepartyGetResponse extends Vector<ResponsiblePartyFacade> implements KvmSerializable {

private static final long serialVersionUID = -8065084038519643055L;

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

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

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

}

@Override
public void setProperty(int index, Object value) {

// add ResponsiblePartyFacade to vector
this.add((ResponsiblePartyFacade) value);

}

ResponsiblePartyFacade:现在我注释掉了 vecor 项,因为它们不在响应中,因为我认为它可能有效(因为网络上有几个博客说 ksoap2 向量不起作用)。

public final class ResponsiblePartyFacade  implements KvmSerializable {
private Integer id;
private String name;
private String discriminator;
private Boolean admin;
// private List<PairIntBoolFacade> children; // not used
// private List<Integer> plantcomponents; // not used


public ResponsiblePartyFacade() {
}


public Integer getId() {
return id;
}



public void setId(Integer id) {
this.id = id;
}


public String getName() {
return name;
}



public void setName(String name) {
this.name = name;
}


public String getDiscriminator() {
return discriminator;
}


public void setDiscriminator(String discriminator) {
this.discriminator = discriminator;
}

public Boolean getAdmin() {
return admin;
}


public void setAdmin(Boolean admin) {
this.admin = admin;
}

/*
public List<PairIntBoolFacade> getChildren() {
if (children == null) {
children = new ArrayList<PairIntBoolFacade>();
}
return children;
}


public void setChildren(List<PairIntBoolFacade> children) {
this.children = children;
}



public List<Integer> getPlantcomponents() {
if (plantcomponents == null) {
plantcomponents = new ArrayList<Integer>();
}
return plantcomponents;
}


public void setPlantcomponents(List<Integer> plantcomponents) {
this.plantcomponents = plantcomponents;
}
*/



public int getPropertyCount() {
return 4;
}



public Object getProperty(int __index) {
switch(__index) {
case 0: return id;
case 1: return name;
case 2: return discriminator;
case 3: return admin;
//case 4: return children;
//case 5: return plantcomponents;
}
return null;
}

public void setProperty(int __index, Object value) {
switch(__index) {
case 0: id = Integer.parseInt(value.toString()); break;
case 1: name = value.toString(); break;
case 2: discriminator = value.toString(); break;
case 3: admin = Boolean.parseBoolean(value.toString()); break;
//case 4: children = (List) __obj; break;
//case 5: plantcomponents = (List) __obj; break;


}
}

public void getPropertyInfo(int __index, Hashtable __table, PropertyInfo __info) {
switch(__index) {
case 0:
__info.name = "id";
__info.type = Integer.class; break;
case 1:
__info.name = "name";
__info.type = String.class; break;
case 2:
__info.name = "discriminator";
__info.type = String.class; break;
case 3:
__info.name = "admin";
__info.type = Boolean.class; break;
/*
case 4:
__info.name = "children";
__info.type = PropertyInfo.VECTOR_CLASS;
case 5:
__info.name = "plantcomponents";
__info.type = PropertyInfo.VECTOR_CLASS;
*/
}
}

}

这是我的网络服务调用:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

// For testing I only want to have 3 results
request.addProperty("limit", "3");


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

// add mapping
// ResponsiblepartyGetResponse got namespace in XML
envelope.addMapping(NAMESPACE, "ResponsiblepartyGetResponse", new ResponsiblepartyGetResponse().getClass());

// ResponsiblePartyFacades doesn't have namespace. I also tried to add namespace, but same result
envelope.addMapping("", "ResponsiblePartyFacades", new ResponsiblePartyFacade().getClass());

ResponsiblepartyGetResponse result = new ResponsiblepartyGetResponse();

try {
AuthTransportSE androidHttpTransport = new AuthTransportSE(URL, USERNAME, PASSWORD);
androidHttpTransport.debug = true;

androidHttpTransport.call(SOAP_ACTION, envelope);

// this shows me my result as a string, see above
System.out.println(envelope.bodyIn.toString());

// Trying to case the response -> Exception
result = (ResponsiblepartyGetResponse) envelope.bodyIn;

} catch (Exception e){
Log.d("SOAPEnvelope", "Error: "+e.toString());
}

return result;

如某些教程所示,我还尝试手动解析响应。但是为此,必须像这样将响应转换为“SoapObject”

SoapObject response = (SoapObject)envelope.getResponse();

当我这样做时,出现以下异常:

05-11 07:09:56.389: D/SOAPEnvelope(2209): Error: java.lang.ClassCastException: java.util.Vector cannot be cast to org.ksoap2.serialization.SoapObject
05-11 07:09:56.402: D/SOAPEnvelope(2209): java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

有人可以给我提示吗?

最佳答案

再绝望一天后,我在 java.lang.ClassCastException: org.ksoap2.serialization.SoapObject 中找到了一个可以追溯到“misco”的灵魂。

我不再尝试转换响应,而是自己手动构建列表:

androidHttpTransport.call(SOAP_ACTION, envelope);
ArrayList<ResponsiblePartyFacades> returnlist = new ArrayList<ResponsiblePartyFacades>();

java.util.Vector<SoapObject> rs = (java.util.Vector<SoapObject>) envelope.getResponse();

if (rs != null)
{
for (SoapObject cs : rs)
{
ResponsiblePartyFacades rp = new ResponsiblePartyFacades();

rp.setId(Integer.parseInt(cs.getProperty(0).toString()));

rp.setName(cs.getProperty(1).toString());
rp.setDiscriminator(cs.getProperty(2).toString());
rp.setAdmin(Boolean.parseBoolean(cs.getProperty(3).toString()));

Log.d("WS", "ID = "+rp.getId() +" name = "+rp.getName()+" disc = "+rp.getDiscriminator()+" admin = "+rp.getAdmin().toString() );

returnlist.add(rp);
}
}

关于android - KSOAP2 处理复杂响应(矢量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16494823/

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