2012-10-03 51 views
2

我的SOAP請求看起來像這樣ksoap2:郵政根據這一要求XML

<?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登錄,但我還沒有想出圍繞身體部位SOAP請求,任何想法如何使用參數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(); 
      }  

功能準備標頭值進行驗證

​​

回答

0

我只是在尋找一種方式來發布正文消息。

您只需要創建一個SoapObject並將其傳遞給信封的bodyOut屬性。

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

你可以請檢查並幫助解決我的問題嗎? http://stackoverflow.com/questions/34670782/android-call-soap-webservice-getting-message-of-server-was-unable-to-process-re?noredirect=1#comment57087199_34670782 – Android

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

感謝您的回覆,但Useraname和密碼必須在Authenticator元素下,並且該部分已在工作。不工作的部分是帶參數的Body中的函數。 – Sharj

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

    } 
}