2017-08-09 100 views
1

我需要根據標題所述發送請求。我有一個完整的請求文本,這裏是: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。

回答

0

好吧,我得到了解決,這是我創建的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; 
      } 
     } 
    } 
} 

這工作得很好。

相關問題