创建SOAP请求标头

时间:2014-08-13 12:03:08

标签: java android soap ksoap2

我想通过使用kso​​ap2-android-assembly-2.5.8-jar-with-dependencies.jar以编程方式创建具有以下结构的SOAP请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:con="http://www.google.com/Consumer/">
   <soapenv:Header>
      <con:RequestHeader>
         <!--Optional:-->
         <serviceId></serviceId>
         <serviceCode>CUSTREG</serviceCode>
         <phaseCode>MOBREG</phaseCode>
         <subTransactionCode>MOBREG</subTransactionCode>
         <interactionCode>LOGIN</interactionCode>
         <!--Optional:-->
         <bearer></bearer>
         <!--Optional:-->
         <clientRefId></clientRefId>
      </con:RequestHeader>
   </soapenv:Header>
   <soapenv:Body>
      <con:Login>
         <LoginRequest>
            <uniqueId>+911234567890</uniqueId>
            <password>123456</password>
            <!--Optional:-->
            <uuid></uuid>

         </LoginRequest>
      </con:Login>
   </soapenv:Body>
</soapenv:Envelope>

先谢谢。

1 个答案:

答案 0 :(得分:0)

public class GetLogin extends AsyncTask<String, Void, String>
    {
        ProgressDialog pd ;
        public String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; // initialise it with your NAMESPACE
        private Context _context;
        public String URL = "";  // initialise it with your URL
        public GetLogin(Context c)
        {
            _context = c;
        }

        protected void onPreExecute()
        {
            super.onPreExecute();
            pd = new ProgressDialog(_context);
            pd.setTitle("Authenticating");
            pd.setMessage("Please wait...");
            pd.setCancelable(true);
            pd.setIndeterminate(true);
            pd.show();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            if (params[0].trim().equals("")) {
                return "";
            }

            SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, params[0].trim());

            request.addProperty("uniqueId",params[1]);
            request.addProperty("password",params[2]);   // uniqueId and password are method parameters. You should use exactly the way it is.

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.dotNet = true;

            envelope.setOutputSoapObject(request);


            HttpTransportSE httpTransport = new HttpTransportSE(URL);

            try
            {
                httpTransport.call(WSDL_TARGET_NAMESPACE + params[0].trim(), envelope);

                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

                return response.toString();
            }

            catch (Exception e)
            {
                if (pd.isShowing())
                    pd.dismiss();

            }

        }


        protected void onPostExecute(String str)
        {
            super.onPostExecute(str);

                if (pd.isShowing())
                    pd.dismiss();
            // str has the retrieved information from the web service.
        }

等活动中调用它
 new GetLogin(yourActivity.this).execute("MethodName", "Parameter1", "Parameter2");