Вопрос

My soap request looks like this

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

I'm at least successfully able to login using ksoap2 but I haven't figured out about the body part, any ideas how to call ListItems with parameter strCode, like it shows above.

Here's the current code.

     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();
          }     

function for preparing header values for authentication

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

I was just looking for a way to post body message.

All you need is to create a SoapObject and pass it to bodyOut property of envelope.

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

Другие советы

try the following code in AsyncTask of DoInBackground Function:

     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);
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
                }
        });

    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top