gpt4 book ai didi

android - 通过 Retrofit 2 发送 SOAP-XML 请求和获取 SOAP-XML 响应

转载 作者:行者123 更新时间:2023-11-29 01:08:01 25 4
gpt4 key购买 nike

我需要按照标题所说的发送请求。我有一个完整的请求文本,这里是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="https://api.myapi.org/api">
<soapenv:Header>
<api:Login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<login xsi:type="xsd:string">my_login</login>
<password xsi:type="xsd:string">my_password</password>
</api:Login>
</soapenv:Header>
<soapenv:Body>
<api:GetHouseInfo>
<address>
<houseId>IDHOUSE</houseId>
</address>
</api:GetHouseInfo>
</soapenv:Body>
</soapenv:Envelope>

这里唯一发生变化的是 houseId,我需要在应用程序中选择的每个房子发送新的 houseId。我的应用程序执行大量 JSON 请求,因此我使用 Retrofit 2 来处理 em。所以我想设置模型,从模型创建这个请求并将其发送到服务器。但是怎么办?我在 Internet 上看到了一些信息,但这不是一回事 - 我有两个请求,我会说 - 一个在标题部分(登录名/密码),第二个在正文部分(houseId)。我尝试按原样发​​送字符串,但它不起作用。这是我的 Presenter 代码 (Moxy MVP):

@InjectViewState
public class HouseInfoPresenter extends BasePresenter<HouseInfoView> {

private static final String TAG = HouseInfoPresenter.class.getSimpleName();

public void getHouseInfo(String body) {

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(String.format("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:api=\"https://api.myapi.org/api\"><soapenv:Header><api:Login soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><login xsi:type=\"xsd:string\">my_loging</login><password xsi:type=\"xsd:string\">my_password</password></api:Login></soapenv:Header><soapenv:Body><api:GetHouseInfo><address><houseId>%s</houseId></address></api:GetHouseInfo></soapenv:Body></soapenv:Envelope>", body));

String xml = stringBuilder.toString();


mCoreServices
.getXmlApiService()
.getApi()
.getHouseInfo(xml)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(() -> getViewState().onShowProgressBar(true))
.doOnUnsubscribe(() -> getViewState().onShowProgressBar(false))
.subscribe(s -> {
getViewState().onSuccess("GOOD");
}, new ApiExceptionObservable(TAG) {
@Override
public void call(String message) {
getViewState().onShowErrorMessage(message);
}

@Override
public void unauthorized() {
getViewState().showUnauthorizedDialog();
}
});
}
}

但我什至无法得到正确答案。我以前处理过 XML 请求,但从未使用过 SOAP。

最佳答案

好的,我得到了解决方案,这是我创建的 Java 对象模型:

@Root(name = "soapenv:Envelope", strict = false)
public class XMLHouseInfoRequest {

public XMLHouseInfoRequest(String login, String password, String houseGuid) {
this.rootElement1 = new HouseInfoRequestHeader(login, password);
this.rootElement2 = new HouseInfoRequestBody(houseGuid);
}

@Attribute(name = "xmlns:soapenv")
private String soapenv = "http://schemas.xmlsoap.org/soap/envelope/";

@Attribute(name = "xmlns:api")
private String api = "https://api.myip.org/api";

@Element(name = "soapenv:Header")
public HouseInfoRequestHeader rootElement1;

@Element(name = "soapenv:Body")
public HouseInfoRequestBody rootElement2;

@Root
public static class HouseInfoRequestHeader {
public HouseInfoRequestHeader(String login, String password) {
this.login = new Login(login, password);
}

@Element(name = "api:Login")
public Login login;

@Root
public static class Login {

@Attribute(name = "soapenv:encodingStyle")
private String encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/";

public Login(String login, String password) {
this.login = login;
this.password = password;
}

@Element(name = "login")
private String login;

@Element(name = "password")
private String password;
}
}

@Root
public static class HouseInfoRequestBody {
public HouseInfoRequestBody(String houseGuid) {
this.getHouseInfo = new GetHouseInfo(houseGuid);
}

@Element(name = "api:GetHouseInfo")
public GetHouseInfo getHouseInfo;

@Root
public static class GetHouseInfo {
public GetHouseInfo(String houseGuid) {
this.address = new Address(houseGuid);
}

@Element(name = "address")
public Address address;

@Root
public static class Address {
public Address(String houseguid) {
this.houseguid = houseguid;
}

@Element(name = "houseguid")
private String houseguid;
}
}
}
}

这很好用。

关于android - 通过 Retrofit 2 发送 SOAP-XML 请求和获取 SOAP-XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45583355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com