2

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

<?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>

我至少能够使用 ksoap2 成功登录,但我还没有弄清楚正文部分,任何想法如何使用参数 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;
    }
4

3 回答 3

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);
于 2012-10-03T04:34:10.967 回答
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
                }
        });

    }
}
于 2012-10-03T05:15:19.407 回答
0

我只是在寻找一种发布正文消息的方法。

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

      SoapObject sub = new SoapObject(NAMESPACE, METHOD);
      sub.addProperty("strCode", value);
      envelope.bodyOut = sub;
于 2012-10-04T03:10:37.573 回答