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

时间:2017-08-09 06:43:36

标签: android xml soap request retrofit2

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

<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。所以我想设置模型,从模型创建此请求并将其发送到服务器。但是怎么样?我在互联网上看到了一些信息,但这不是一回事 - 我有两个请求,一个在Header部分(登录/密码),第二个在Body部分(houseId)。 我试图发送字符串,但它不起作用..这是我的演示者代码(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。

1 个答案:

答案 0 :(得分:1)

好的,我得到了解决方案,这是我创建的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;
            }
        }
    }
}

这很好用。