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

问题描述:

我需要根据标题所述发送请求。我有一个完整的请求文本,这里是:android - 发送SOAP-XML请求并通过改进获取SOAP-XML响应2

<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。所以我想设置模型,从模型创建这个请求并将其发送到服务器。但是如何?我在互联网上看到了一些信息,但它不是一回事 - 我有一个要求 - 一个在标题部分(登录名/密码),另一个在正文部分(houseId)。 我试图发送字符串,因为它是,但它不工作..这是我的演示代码(莫西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; 
      } 
     } 
    } 
} 

这工作得很好。