Den
Den

Reputation: 1352

android - Sending SOAP-XML Request and getting SOAP-XML Response via Retrofit 2

I need to send the request as the title says. I have a complete request text, here it is:

<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>

The only thing that is changing here - houseId, I need to send new houseId with every house selected in app. My application do lots of JSON requests so I use Retrofit 2 to work with em. So I want to set up the model, create this request from the model and send it to server. But how? I saw some info in the Internet, but it was not the same thing - I have two requests in one I'd say - one in the Header section (login/password) and the second in the Body section (houseId). I tried to send string as it is, but it doesn't work.. Here's my Presenter code (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();
            }
        });
    }
}

But I can't even get the right answer. I worked with XML requests before, but I never used SOAP.

Upvotes: 2

Views: 2989

Answers (1)

Den
Den

Reputation: 1352

Okay, I got the solution, this is the Java Object Model I've created:

@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;
            }
        }
    }
}

This works fine.

Upvotes: 1

Related Questions