ksoap2:根据此请求XML发布soap请求

时间:2012-10-03 04:31:18

标签: android ksoap2 android-ksoap2

我的肥皂请求看起来像这样

<?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>
    <Authenticator xmlns="http://www.namespacename.com/services/">
      <UserName>string</UserName>
      <Password>string</Password>
    </Authenticator>
  </soap:Header>
  <soap:Body>
    <ListItems xmlns="http://www.namespacename.com/services/">
      <strCode>string</strCode>
    </ListItems>
  </soap:Body>
</soap:Envelope>

我至少成功地能够使用kso​​ap2登录,但我还没有弄清楚身体部分,如何使用参数strCode调用ListItems的任何想法,如上所示。

这是当前的代码。

     String NAMESPACE = "http://www.namespace.com/services/";
     String METHOD_NAME = "ListItems";
     String SOAP_ACTION = "http://www.namespace.com/services/ListItems";
     String URL = "https://www.kupong.se/Services/CouponAPI18.asmx";

     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
              // Enable the below property if consuming .Net service
      envelope.dotNet = true;
      envelope.setOutputSoapObject(request);

      envelope.headerOut = new Element[1];
      envelope.headerOut[0] = buildAuthHeader();

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
          try 
          {
              androidHttpTransport.call(SOAP_ACTION, envelope, null);
              response = (SoapObject)envelope.getResponse();

          }
          catch(Exception e)
          {
              e.printStackTrace();
          }     

用于为身份验证准备标头值的功能

private Element buildAuthHeader() {
            Element h = new Element().createElement(NAMESPACE, "Authenticator");

            Element username = new Element().createElement(NAMESPACE, "UserName");
            username.addChild(Node.TEXT, username);
            h.addChild(Node.ELEMENT, username);
            Element pass = new Element().createElement(NAMESPACE, "Password");
            pass.addChild(Node.TEXT, "password)
            h.addChild(Node.ELEMENT, pass);

            return h;
    }

3 个答案:

答案 0 :(得分:0)

在DoInBackground函数的AsyncTask中尝试以下代码:

     String user=empidedt.getText().toString().trim();
                   String pass=passedt.getText().toString().trim();

                  System.out.println("user :"+user);
                  System.out.println("pass :"+pass);
                try{

                   SoapObject request = new SoapObject(NAMESPACE1, METHOD_NAME1);                      
                   request.addProperty("UserId",""+user);
                   request.addProperty("Password", ""+pass);

                   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                   envelope.dotNet=true;
                   envelope.setOutputSoapObject(request);

                   HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);

                   androidHttpTransport.call(SOAP_ACTION1, envelope);

                   Object result= envelope.getResponse();

                   SoapObject response=(SoapObject)envelope.bodyIn;

                  strRes = result.toString();
                  System.out.println("strRes :"+strRes);

答案 1 :(得分:0)

Using Ksoap2 library and write .net web service 
Sucessful Connection with Asp.net Webservice-----
package ProductVerificationCard.in;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AdminLogin extends Activity {
    /** Called when the activity is first created. */
    Button btn_ok;
    TextView textView;
    private static final String SOAP_ACTION = "http://tempuri.org/Login";

    private static final String OPERATION_NAME = "Login";

    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

    private static final String SOAP_ADDRESS = "http://10.0.2.2/new/WebService.asmx";
    String s;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn_ok=(Button) findViewById(R.id.btn_login);
        textView=(TextView) findViewById(R.id.tv_error);

        btn_ok.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                        OPERATION_NAME);

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                        envelope.dotNet = true;

                        envelope.setOutputSoapObject(request);

                        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

                        try

                        {

                        httpTransport.call(SOAP_ACTION, envelope);

                        Object response = envelope.getResponse();

                        //textView.setText(response.toString());
                         s=response.toString();
                         if(s=="true")
                         {
                             Intent intent=new Intent(AdminLogin.this,MenuForm.class);
                                startActivity(intent);

                         }

                         else
                         {
                             textView.setText("Enter Valid Username or Password");
                         }
                        }

                        catch (Exception exception)

                        {

                        textView.setText(exception.toString());

                        }
                // TODO Auto-generated method stub
                }
        });

    }
}

答案 2 :(得分:0)

我只是在寻找一种发布身体信息的方法。

您只需要创建一个SoapObject并将其传递给envelope的bodyOut属性。

      SoapObject sub = new SoapObject(NAMESPACE, METHOD);
      sub.addProperty("strCode", value);
      envelope.bodyOut = sub;