【发布时间】:2017-01-14 05:54:56
【问题描述】:
我正在尝试使用改造来发出 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;
}
}
}
如您所见,我在 Logon 类上添加了@Order。但是当我尝试运行它时出现以下错误。
Caused by: org.simpleframework.xml.core.ElementException: Ordered element 'type:password' missing for class test.SoapLoginReq$Logon.
我是第一次尝试,我尝试将字符串名称放入 order 元素数组中。喜欢@Order(elements = {"password", "type:username"}) 而不是type:password
还是不行。
这是我的活动代码。
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 在请求中非常重要。
谢谢:)
更新
当我将@Order(elements = {"type:username", "type:password"}) 添加到父元素即登录时
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;
}
}
它没有抛出错误。但排序仍然是个问题。
【问题讨论】:
标签: java android soap retrofit retrofit2