访问android中的soap webservice

时间:2013-07-15 12:05:03

标签: android soap

我正在尝试从soap webservice获得响应,但我总是收到错误,请检查我是否正确地调用了Web服务。 以下是我的webservice wsdl

<wsdl:definitions name="MobileeServiceWsd" targetNamespace="urn:MobileeServiceWsd"><wsdl:import location="http://vddp.dewa.gov.ae:50700/MobileeService/Config2/bindings?wsdl&style=document" namespace="urn:MobileeServiceWsd/Config2/document"/><wsdl:service name="MobileeService"><wsdl:port name="Config2Port_Document" binding="bns0:Config2Binding"><soap:address location="http://vddp.dewa.gov.ae:50700/MobileeService/Config2?style=document"/></wsdl:port></wsdl:service></wsdl:definitions>

以下是我的代码。

 private static final String METHOD_NAME = "getOutstandingBalance";
    private static final String SOAP_ACTION = "MobileeService";
    private static final String NAMESPACE = "MobileeServiceWsd/Config2/document";
    private static final String URL = "http://vddp.dewa.gov.ae:50700/MobileeService/Config2/bindings?wsdl&style=document";


    public SoapObject testMethod(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE,   String URL) throws IOException, XmlPullParserException
    {
        //List<HeaderProperty> headers = new ArrayList<HeaderProperty>();

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true; //for .net services
        envelope.bodyOut = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:MobileeServiceVi\">"+
                   "<soapenv:Header/>"+
                   "<soapenv:Body>"+
                      "<urn:getOutstandingBalance>"+
                         "<urn:contractaccount>000001000104</urn:contractaccount>"+
                         "<urn:mobileosver>ios 4.3</urn:mobileosver>"+
                     "<urn:appver>3.3</urn:appver>"+
                         "<urn:appidentifier>343534</urn:appidentifier>"+
                      "</urn:getOutstandingBalance>"+
                   "</soapenv:Body>"+
                "</soapenv:Envelope>";
        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(URL);

        //httpTransport.call(SOAP_ACTION, envelope, headers);
        httpTransport.call(SOAP_ACTION, envelope);

        return (SoapObject) envelope.getResponse();
    }

1 个答案:

答案 0 :(得分: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
                }
        });

    }
}