如何在 Android 中进行 SOAP 解析?

How to SOAP Parsing in Android?

我是这个 SOAP 网站的新手service.This 是我的 ksoap 库依赖项,

 repositories {
    maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
}
dependencies {
    compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.1'
}

请注意我的 SOAP 请求和响应

 POST /loyaltywebservice/LoyaltyWebService.asmx HTTP/1.1
Host: host url is here
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/AttemptLogin"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <UserCredential xmlns="http://tempuri.org/">
      <userName>string</userName>
      <password>string</password>
    </UserCredential>
  </soap:Header>
  <soap:Body>
    <AttemptLogin xmlns="http://tempuri.org/">
      <LoyalCustCode>string</LoyalCustCode>
      <PwdStr>string</PwdStr>
    </AttemptLogin>
  </soap:Body>
</soap:Envelope>

在解析时正在获取 Web 服务身份验证 fail.But,它在 "POSTMAN" 中工作。我尝试了以下代码,并且我刚刚更改了用户名和密码,因为我无法打开credential.So 我刚刚添加了 1234.Have 看看我的 code.And 请帮助我哪里做错了。

    public void soapLogin() {
    try {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    //Prepare the header with the authentication data.
                    Element headers = new Element().createElement(UrlActivity.NAMESPACE, "UserCredential");

                    Element username = new Element().createElement(UrlActivity.NAMESPACE, "userName");
                    username.addChild(Node.TEXT, "test");
                    headers.addChild(Node.ELEMENT, username);

                    Element pass = new Element().createElement(UrlActivity.NAMESPACE, "password");
                    pass.addChild(Node.TEXT, "1234");
                    headers.addChild(Node.ELEMENT, pass);

                    // Soap Request
                    final SoapObject request = new SoapObject(UrlActivity.NAMESPACE, UrlActivity.METHOD_NAME);
                    request.addProperty("LoyalCustCode","12345");
                    request.addProperty("PwdStr","12345");

                    final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    // add header to envelope
                    envelope.headerOut = new Element[1];
                    envelope.headerOut[0] = headers;
                    envelope.setOutputSoapObject(request);

                    //Add the header to the envelope.
                    final HttpTransportSE androidHttpTransport = new HttpTransportSE(UrlActivity.URL);
                    androidHttpTransport.debug = true;
                    androidHttpTransport.call(UrlActivity.SOAP_ACTION, envelope);
                    Log.d("Login", "Request:- "+androidHttpTransport.requestDump);

                    final SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
                    Log.d("Login", "Response:- "+androidHttpTransport.responseDump);
                    // final SoapObject result = (SoapObject) envelope.getResponse();
                    String response = result.toString();
                    Log.d("Login", "Response:- "+response);
                }catch (Exception ex){
                    Log.e("Login", "soapLogin:- "+ex.getMessage());
                }
            }
        }).start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
    final SoapObject request = new SoapObject(UrlActivity.NAMESPACE, UrlActivity.METHOD_NAME);
                        // TODO the two params are child soap objects not properties
                        SoapObject loyal = new SoapObject(UrlActivity.NAMESPACE, "LoyalCustCode");
                        loyal.setInnerText("1234");
                        request.addSoapObject(loyal);

                        loyal = new SoapObject(UrlActivity.NAMESPACE, "PwdStr");
                        loyal.setInnerText("1234");
                        request.addSoapObject(loyal);
//Params are different than child node

首先您需要将这些添加到您的 build.gradle

repositories {
    maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
}
dependencies {
    compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.1'
}

然后使用下面的代码获取 API Response-

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapObject loyal = new SoapObject(NAMESPACE, "LoyalCustCode");
            loyal.setInnerText("custcode");
            request.addSoapObject(loyal);

            loyal = new SoapObject(NAMESPACE, "PwdStr");
            loyal.setInnerText("pass");
            request.addSoapObject(loyal);

            Element headers = new Element().createElement(NAMESPACE, "UserCredential");

            Element username = new Element().createElement(NAMESPACE, "userName");
            username.addChild(Node.TEXT, "test");
            headers.addChild(Node.ELEMENT, username);

            Element pass = new Element().createElement(NAMESPACE, "password");
            pass.addChild(Node.TEXT, "1234");
            headers.addChild(Node.ELEMENT, pass);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.headerOut = new Element[1];
            envelope.headerOut[0] = headers;
            envelope.setOutputSoapObject(request);

            HttpTransportSE ht = new HttpTransportSE(URL);
            try {
                ht.call(SOAP_ACTION, envelope);
                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                resultado = response.toString();
                Log.i("Resultado: ", resultado);
            } catch (Exception e) {
                Log.i("Error: ", e.getMessage());
                e.printStackTrace();
                return false;
            }