无法使用SimpleXML反序列化SOAP响应?

时间:2016-12-07 11:30:33

标签: android retrofit2 simple-framework

我正在开发一个与本地设备进行UPnP连接的库。 当我尝试解析其中一个操作的响应时,我得到以下异常:

问题: org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=s:Body, required=true, type=class com.stuff.AssignedRolesResponseBody) on field 'responseBody' private com.stuff.AssignedRolesResponseBody com.stuff.AssignedRolesResponseEnvelope.responseBody for class com.stuff.AssignedRolesResponseEnvelope at line 1

我想解析的

原始回复

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <s:Body>
        <u:GetAssignedRolesResponse xmlns:u="urn:schemas-upnp-org:service:DeviceProtection:1">
            <RoleList>{something_here?}</RoleList>
        </u:GetAssignedRolesResponse>
    </s:Body>
</s:Envelope>

这些是我的POJO:

ResponseEnvelope:

@Root(name = "s:Envelope")
@NamespaceList({
        @Namespace(prefix = "s", reference = "http://schemas.xmlsoap.org/soap/envelope/")
})
public class AssignedRolesResponseEnvelope extends XMLBaseResponse {

  @Element(name = "s:Body", type = AssignedRolesResponseBody.class)//I tried without specifiying the type here - no difference
  private AssignedRolesResponseBody responseBody;

  public AssignedRolesResponseBody getResponseBody() {
    return responseBody;
  }

  public void setResponseBody(AssignedRolesResponseBody responseBody) {
    this.responseBody = responseBody;
  }
}

体:

public class AssignedRolesResponseBody {

  @Element(name = "u:GetAssignedRolesResponse")
  @NamespaceList({
          @Namespace(prefix = "u", reference = "urn:schemas-upnp-org:service:DeviceProtection:1")
  })
  private AssignedRolesResponseAction action;

  public AssignedRolesResponseAction getAction() {
    return action;
  }

  public void setAction(AssignedRolesResponseAction action) {
    this.action = action;
  }
}

动作:

public class AssignedRolesResponseAction {

  @Element(name = "RoleList")
  List<String> roleList;

  public List<String> getRoleList() {
    return roleList;
  }

  public void setRoleList(List<String> roleList) {
    this.roleList = roleList;
  }
}

非常感谢任何输入。

1 个答案:

答案 0 :(得分:1)

我会回答我自己的问题。我做了3次修改来解决这个问题:

1)。也映射了encodyngStyle,如下所示:

  @Attribute(name = "encodingStyle")
  public String encodingStyle;

2)。映射其他实体,不带前缀:

  @Element(name = "Body")
  private AssignedRolesResponseBody responseBody;

  @Element(name = "GetAssignedRolesResponse")
  private AssignedRolesResponseAction action;

3)。映射操作

root
@Root(name = "u:GetAssignedRolesResponse")
@Namespace(reference = "urn:schemas-upnp-org:service:DeviceProtection:1", prefix = "u")
public class AssignedRolesResponseAction {}