将参数传递到KSOAP2

时间:2018-08-14 18:30:55

标签: java android ksoap2

在此先感谢您抽出时间阅读此问题。我正在尝试使用kso​​ap2将android应用程序与SOAP webservice集成。

这是我要提出的示例请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns="http://clients.mindbodyonline.com/api/0_5_1">
           <soapenv:Header/>
           <soapenv:Body>
             <ValidateStaffLogin>
               <Request>
                 <SourceCredentials>
                   <SourceName>JohnDoe</SourceName>
                   <Password>123ABCFAKEAPIKEY123</Password>
                   <SiteIDs>
                     <int>-99</int>
                   </SiteIDs>
                 </SourceCredentials>
                 <Username>Siteowner</Username>
                 <Password>apitest1234</Password>
               </Request>
             </ValidateStaffLogin>
           </soapenv:Body>
         </soapenv:Envelope>

这是webservice java文件的摘录:

public class WebService {
//Namespace of the Webservice - can be found in WSDL
private static String NAMESPACE = 
    "http://clients.mindbodyonline.com/api/0_5_1";
//Webservice URL - WSDL File location
private static String URL = 
    "https://api.mindbodyonline.com/0_5_1/StaffService.asmx";
//SOAP Action URI again Namespace + Web method name
private static String SOAP_ACTION = 
    "http://clients.mindbodyonline.com/api/0_5_1";

public static boolean invokeLoginWS(String userName,String passWord, String 
webMethName) {
    boolean loginStatus = false;
    // Create request
    SoapObject request = new SoapObject(NAMESPACE, webMethName);

    request.addProperty("SourceCredentials", "SourceName", "JohnDoe");
    request.addProperty("SourceCredentials", "Password", 
        "123ABCFAKEAPIKEY123");
    request.addProperty("SourceCredentials", "SiteIDs", "-99");
    request.addProperty("Username", "Siteowner");
    request.addProperty("Password", "apitest1234");


    // Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    // Set output SOAP object
    envelope.setOutputSoapObject(request);
    // Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        // Invoke web service
        androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
        // Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to  boolean variable variable
        loginStatus = Boolean.parseBoolean(response.toString());

    } catch (Exception e) {
        //Assign Error Status true in static variable 'errored'
        LoginActivity.errored = true;
        e.printStackTrace();
    }
    //Return booleam to calling object
    return loginStatus;
}
}

我的问题是,我究竟应该如何将一个不同的变量传递到两个单独的密码字段中。我找到了ksoap2方法addProperty(命名空间,名称,值),但是我不确定这是否是正确的方法。

我对NAMESPACE,URL和SOAP_ACTION变量以及如何正确填写它们也有些困惑。

1 个答案:

答案 0 :(得分:0)

    SoapObject request = new SoapObject(ApiUrl.NAMESPACE,
            ApiUrl.METHODNAME_LOGIN);

SoapSerializationEnvelope soapenvelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    soapenvelope.dotNet = true;
    soapenvelope.bodyIn = false;

    HttpTransportSE androidHttpTransport = new HttpTransportSE(
            ApiUrl.DMATURL);
    androidHttpTransport.debug = true;
soapenvelope.setAddAdornments(false);
    androidHttpTransport
            .setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    // HttpTransportSE transport = new HttpTransportSE(URL);
    try {
        androidHttpTransport.call(ApiUrl.SOAPACTION_LOGIN, soapenvelope);
        String responss = androidHttpTransport.responseDump;

        //System.out.println("request==" +androidHttpTransport.requestDump);
    //  System.out.println("responss===" + responss);

        SoapObject resultRequestSOAP = (SoapObject) soapenvelope
                .getResponse();
        SoapObject response = (SoapObject) soapenvelope.bodyIn;
}
                catch(Exception ex){
                    ex.printStackTrace();   
                }