我的服务通常会返回类似以下内容:
anyType{Acc1=96628; Code=E; OnArr=false; CanArr=true; Username=ANDERSON;}
然后我将其推向我的对象
details.Username = response.getProperty("Username").toString();
这就像我需要的那样有效,直到我收到没有用户名值的响应,返回如下:
anyType{Acc1=96628; Code=E; OnArr=false; CanArr=true; Username=anyType{};}
当我稍后调用details.Username时,它返回“anyType{}”(作为字符串),而不是返回空字符串
你能告诉我我错过了什么吗?
为了清楚地说明我如何得到回复
...
SoapObject request = new SoapObject(NAMESPACE, methodName);
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setName("Details");
propertyInfo.setValue(details);
propertyInfo.setType(details.getClass());
request.addProperty(propertyInfo);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, "Details", Details.class);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
httpTransportSE.call(NAMESPACE + methodName, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
[编辑]
以下内容似乎已经成功;虽然,我不太确定为什么或者这是否是正确的方法。
details.Username = response.getPrimitivePropertyAsString("Username");
最佳答案
这似乎已解决
details.Username = response.getPrimitivePropertyAsString("Username");
深入研究SoapObject.java,我也能看到它背后的意义
/**
* Get the toString value of a property without chance of throwing an
* exception. An object can be provided to this method; if the property is
* not found, this object's string representation will be returned.
*
* @param defaultThing
* toString of the object to return if the property is not found
* @return the property toString if it exists; defaultThing toString if the
* property does not exist, if the defaultThing is null #EMPTY_STRING
* is returned
*/
public String getPropertySafelyAsString(final String namespace,final String name,
final Object defaultThing) {
Integer i = propertyIndex(namespace,name);
if (i != null) {
Object property = getProperty(i.intValue());
if (property != null) {
return property.toString();
} else {
return EMPTY_STRING;
}
} else {
if (defaultThing != null) {
return defaultThing.toString();
} else {
return EMPTY_STRING;
}
}
}
关于java - 如何正确处理来自Web服务的anyType()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55025580/