java - 带有 Retrofit2 订单问题的 SOAP

标签 java android soap retrofit retrofit2

我正在尝试使用改造来发出 SOAP 请求。除了元素的排序/顺序外,一切正常。

这是我的 POJO 代码

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.NamespaceList;
import org.simpleframework.xml.Order;
import org.simpleframework.xml.Root;

/**
 * Created by ankit on 13/01/17.
 */

@Root(name = "soapenv:Envelope")
@NamespaceList({
        @Namespace(prefix = "soapenv", reference = "http://schemas.xmlsoap.org/soap/envelope/"),
        @Namespace(prefix = "type", reference = "http://tempuri.org/type")
})

public class SoapLoginReq {

    @Element(name = "soapenv:Body")
    private SoapBodyReq soapBodyReq;

    public SoapLoginReq(String username, String password){
        soapBodyReq = new SoapBodyReq(new Logon(username, password));
    }

    public SoapBodyReq getSoapBodyReq() {
        return soapBodyReq;
    }

    public void setSoapBodyReq(SoapBodyReq soapBodyReq) {
        this.soapBodyReq = soapBodyReq;
    }

    static class SoapBodyReq{

        @Element(name = "type:logon")
        private Logon logon;

        public SoapBodyReq(Logon logon){
            this.logon = logon;
        }

        public Logon getLogon() {
            return logon;
        }

        void setLogon(Logon logon) {
            this.logon = logon;
        }
    }

    @Order(elements = {"type:password", "type:username"})
    static class Logon{
        @Element(name = "type:username")
        private String username;

        @Element(name = "type:password")
        private String password;

        public Logon(String username, String password){
            this.username = username;
            this.password = password;
        }

        public String getUsername() {
            return username;
        }

        void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        void setPassword(String password) {
            this.password = password;
        }
    }
}

如您所见,我在登录类上添加了@Order。但是当我尝试运行它时出现以下错误。

原因:org.simpleframework.xml.core.ElementException:类 test.SoapLoginReq$Logon 缺少有序元素“type:password”

我是第一次尝试制作它,我尝试将字符串名称放入顺序元素数组中。像 @Order(elements = {"password", "type:username"}) 而不是 type:password

还是不行。

这是我的 Activity 代码。

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();

interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

Strategy strategy = new AnnotationStrategy();


Serializer serializer = new Persister(strategy);

OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .addInterceptor(interceptor)
        .connectTimeout(2, TimeUnit.MINUTES)
        .writeTimeout(2, TimeUnit.MINUTES)
        .readTimeout(2, TimeUnit.MINUTES)
        .build();

Retrofit retrofit =  new Retrofit.Builder()
        .addConverterFactory(SimpleXmlConverterFactory.create(serializer))
        .baseUrl("https://requestb.in/")
        .client(okHttpClient)
        .build();

WWIFaces wwiFaces = retrofit.create(WWIFaces.class);

wwiFaces.logon(new SoapLoginReq("ankit", "pise")).enqueue(new Callback<String>() {
    @Override
    public void onResponse(Call<String> call, Response<String> response) {

    }

    @Override
    public void onFailure(Call<String> call, Throwable t) {
        t.printStackTrace();
    }
});

我以前从事过 REST,但 SOAP 对我来说是新事物,而且顺序在请求中似乎非常重要。

谢谢:)

更新

当我添加 @Order(elements = {"type:username", "type:password"}) 到父元素,即 Logon

static class SoapBodyReq{

        @Element(name = "type:logon")
        @Order(elements = {"type:username", "type:password"})
        private Logon logon;

        public SoapBodyReq(Logon logon){
            this.logon = logon;
        }

        public Logon getLogon() {
            return logon;
        }

        void setLogon(Logon logon) {
            this.logon = logon;
        }
    }

它没有抛出错误。但排序仍然是一个问题。

最佳答案

据我所知,@Order 从不与涉及的 namespace 前缀一起使用。将它们全部删除,@Order 应该可以工作。

如果您需要 namespace 前缀,默认情况下 SimpleXML 按字母顺序组织元素(使用变量名称 而不是 @Element 名称标签),因此唯一的选择是按您想要的字母顺序命名变量。在这种情况下,为用户名添加前缀“a”应该将 type:username 放在第一位。

static class Logon{
    @Element(name = "type:username")
    private String aUsername;

    @Element(name = "type:password")
    private String password;

    public Logon(String username, String password){
        this.aUsername= username;
        this.password = password;
    }

    public String getUsername() {
        return aUsername;
    }

    void setUsername(String username) {
        this.aUsername = username;
    }

    public String getPassword() {
        return password;
    }

    void setPassword(String password) {
        this.password = password;
    }
}

关于java - 带有 Retrofit2 订单问题的 SOAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41647232/

相关文章:

java - Java 中的自动装箱和加宽

android - 更改 pubspec.yaml 中的字体系列名称

Android Sherlock VS AppCompat

php - 提供股票市场信息的网络服务

java - 无法在我的 firebase 服务中解析方法 'findViewById(int)'

java - 在 postgresql 中通过 hibernate 保存 java 日期时的 GWT 日期选择器格式问题

java - 检查字符串中的位数

java - 如何使用 Java 生成 SAML 安全 SOAP 请求并使用 SAAJ 调用永恒的 Web 服务

c++ - 测试 gSOAP 服务器

java - 直接访问 Thrift 生成的 Java 类的字段,而不是通过提供的 getter