使用Retrofit 2和Simple XML Converter

时间:2017-04-17 19:11:22

标签: android xml soap retrofit simple-framework

我正在使用Retrofit 2.2.0和Retrofit SimpleXML Converter 2.2.0。我使用SimpleXmlConverter方法将Retrofit添加到addConverterFactory实例。

问题是,当我收到回复时,会收到以下错误

  

java.lang.RuntimeException:org.simpleframework.xml.core.ElementException:Element' Body'在第1行的ResponseEnvelope类中没有匹配项

我应该得到这样的XML响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns:autenticarUsuarioPorEmailResponse xmlns:ns="http://business.curitiba.org.br">
         <ns:return xsi:type="ax2471:AutenticaUsuarioPorEmailSaida" xmlns:ax2471="http://saidas.curitiba.org/xsd" xmlns:ax2469="http://entities.curitiba.org/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ax2467="http://entradas.curitiba.org/xsd">
            <ax2471:idCredencial>3282</ax2471:idCredencial>
            <ax2471:tokenAcesso>635E3DA9-7C02-4DB7-9653-E7688C66B02C</ax2471:tokenAcesso>
         </ns:return>
      </ns:autenticarUsuarioPorEmailResponse>
   </soapenv:Body>
</soapenv:Envelope>

ResponseEnvelope.java

@Root(name = "soapenv:Envelope")
@Namespace(prefix = "soapenv", reference = "http://schemas.xmlsoap.org/soap/envelope/")
public class ResponseEnvelope {

    @Element(name = "soapenv:Body", required = false)
    private ResponseBody body;

    public ResponseBody getBody() {
        return body;
    }

    public void setBody(ResponseBody body) {
        this.body = body;
    }
}

ResponseBody.java

@Root(name = "soapenv:Body", strict = false)
public class ResponseBody {

    @Element(name = "ns:cadastrarCredencialEmailResponse", required = false)
    private ResponseData requestData;

    public ResponseData getRequestData() {
        return requestData;
    }

    public void setRequestData(ResponseData requestData) {
        this.requestData = requestData;
    }

}

ResponseData.java

@Root(name = "ns:cadastrarCredencialEmailResponse", strict = false)
@Namespace(prefix = "ns", reference = "http://business.curitiba.org")
public class ResponseData {

    @Element(name = "ns:return", required = false)
    private ResponseInfo info;

    public ResponseInfo getInfo() {
        return info;
    }

    public void setInfo(ResponseInfo info) {
        this.info = info;
    }
}

ResponseInfo.java

@Root(name = "ns:return", strict = false)
@NamespaceList({
        @Namespace(prefix = "ax2471", reference = "http://saidas.curitiba.org/xsd"),
        @Namespace(prefix = "ax2469", reference = "http://entities.curitiba.org/xsd"),
        @Namespace(prefix = "xsi", reference = "http://www.w3.org/2001/XMLSchema-instance"),
        @Namespace(prefix = "ax2467", reference = "http://entradas.curitiba.org/xsd")
})
public class ResponseInfo {

    @Element(name = "ax2471:codigoAtivacao", required = false)
    private String codigoAtivacao;
    @Element(name = "ax2471:idCredencial", required = false)
    private String idCredencial;

    public String getCodigoAtivacao() {
        return codigoAtivacao;
    }

    public void setCodigoAtivacao(String codigoAtivacao) {
        this.codigoAtivacao = codigoAtivacao;
    }

    public String getIdCredencial() {
        return idCredencial;
    }

    public void setIdCredencial(String idCredencial) {
        this.idCredencial = idCredencial;
    }
}

我想问题出在ResponseInfo课程中,我不知道如何将属性xsi:type放入其中。有人能帮助我吗?

1 个答案:

答案 0 :(得分:7)

我不知道Simple XML Converter是如何工作的,但元素名称是元素名称,它们可以是前缀。这似乎是混乱,因为 $args = array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => 0 ); $product_cat = get_terms( $args ); foreach ($product_cat as $parent_product_cat) { echo ' <ul> <li><a href="'.get_term_link($parent_product_cat->term_id).'">'.$parent_product_cat->name.'</a> <ul> '; $child_args = array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => $parent_product_cat->term_id ); $child_product_cats = get_terms( $child_args ); foreach ($child_product_cats as $child_product_cat) { echo '<li><a href="'.get_term_link($child_product_cat->term_id).'">'.$child_product_cat->name.'</a></li>'; } echo '</ul> </li> </ul>'; } 字符用于在物理XML文档(标记)中分隔名称空间前缀和元素名称。

你可以删除&#34;前缀&#34;从名称中添加:注释。例如:

ResponseEnvelope.java

@Namespace

ResponseBody.java

@Root(name = "Envelope")
@Namespace(prefix = "soapenv", reference = "http://schemas.xmlsoap.org/soap/envelope/")
final class ResponseEnvelope {

    @Element(name = "Body", required = false)
    @Namespace(prefix = "soapenv", reference = "http://schemas.xmlsoap.org/soap/envelope/")
    final ResponseBody body;

    private ResponseEnvelope(
            @Element(name = "Body") final ResponseBody body
    ) {
        this.body = body;
    }

}

ResponseData.java

@Root(name = "Body", strict = false)
@Namespace(prefix = "soapenv", reference = "http://schemas.xmlsoap.org/soap/envelope/")
final class ResponseBody {

    @Element(name = "autenticarUsuarioPorEmailResponse", required = false)
    @Namespace(prefix = "ns", reference = "http://business.curitiba.org")
    final ResponseData requestData;

    private ResponseBody(
            @Element(name = "autenticarUsuarioPorEmailResponse") final ResponseData requestData
    ) {
        this.requestData = requestData;
    }

}

ResponseInfo.java

@Root(name = "autenticarUsuarioPorEmailResponse", strict = false)
@Namespace(prefix = "ns", reference = "http://business.curitiba.org")
final class ResponseData {

    @Element(name = "return", required = false)
    @Namespace(prefix = "ns", reference = "http://business.curitiba.org")
    final ResponseInfo info;

    private ResponseData(
            @Element(name = "return") final ResponseInfo info
    ) {
        this.info = info;
    }

}

以下示例:

@Root(name = "return", strict = false)
@Namespace(prefix = "ns", reference = "http://business.curitiba.org")
final class ResponseInfo {

    @Element(name = "tokenAcesso", required = false)
    @Namespace(prefix = "ax2471", reference = "http://saidas.curitiba.org/xsd")
    final String accessToken;

    @Element(name = "idCredencial", required = false)
    @Namespace(prefix = "ax2471", reference = "http://saidas.curitiba.org/xsd")
    final String credentialId;

    private ResponseInfo(
            @Element(name = "tokenAcesso") final String accessToken,
            @Element(name = "idCredencial") final String credentialId
    ) {
        this.accessToken = accessToken;
        this.credentialId = credentialId;
    }

}

将输出:

  

635E3DA9-7C02-4DB7-9653-E7688C66B02C
  3282