2014-04-22 78 views
0

我想從Android應用程序到PHP Web服務發送數據,Web服務得到這樣的論點:安卓:如何發送數組作爲參數WSDL請求

Type  Name   Description 
string  sessionId  Session ID 
array  customerData Array of customerCustomerEntityToCreate 

我使用kso​​ap2庫,在此代碼我通過的sessionId但我不`噸知道如何設置數組作爲參數到WSDL請求

   env = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
       env.dotNet = false; 
       env.xsd = SoapSerializationEnvelope.XSD; 
       env.enc = SoapSerializationEnvelope.ENC; 

       SoapObject request = new SoapObject(NAMESPACE, 
         "createCustomer"); 
       request.addProperty("sessionId", "1234567890"); 

       env.setOutputSoapObject(request); 
       androidHttpTransport = new HttpTransportSE(URL); 
       androidHttpTransport.debug = true; 

       // (new MarshalHashtable()).register(env); 
       androidHttpTransport.call("", env); 
       result = env.getResponse(); 

       Log.d("result", result.toString()); 

我發現使用Web服務這個PHP例如用PHP代碼:

$client = new SoapClient('http://magentohost/api/v2_soap/?wsdl'); 
$session = $client->login('apiUser', 'apiKey'); 
$result = $client->customerCustomerCreate($session, array('email' => 'customer- [email protected]', 'firstname' => 'Dough', 'lastname' => 'Deeks', 'password' => 'password', 'website_id' => 1, 'store_id' => 1, 'group_id' => 1)); 
var_dump ($result); 

我如何用java做到這一點? 感謝

回答

0
SoapObject request = new SoapObject(NAMESPACE,"login"); 

request.addProperty("username", "*****"); 
request.addProperty("apiKey", "********"); 
env.setOutputSoapObject(request); 

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
androidHttpTransport.call(" ", env); 
Object result = env.getResponse(); 

Log.d("This is the New SessionId", result.toString()); 

String f_name=fname.getText().toString(); 
String l_name=lname.getText().toString(); 
String e_mail=email.getText().toString(); 
String c_pass=cpass.getText().toString(); 
//calling the soap api method "customerCustomerCreate" 
SoapObject res=new SoapObject(NAMESPACE, METHODNAME); 
res.addProperty("email",e_mail); 
res.addProperty("firstname",f_name); 
res.addProperty("lastname",l_name); 
res.addProperty("password",c_pass); 
res.addProperty("website_id", 1); 
res.addProperty("store_id",1); 
res.addProperty("Group_id",1); 

String sessionId = result.toString(); 
request = new SoapObject(NAMESPACE, METHODNAME); 
//adding the propery such as sessionId and Customerdata for request 
request.addProperty("sessionId",sessionId); 
request.addProperty("customerData",res); 
env.setOutputSoapObject(request); 
androidHttpTransport.debug=true; 
androidHttpTransport.call("", env); 
//getting the response which is the customerId 
result=env.getResponse(); 

Log.d("Customer Id", result.toString()); 
+0

在StackOverflow中,我們習慣於添加一些解釋代碼如何工作的習慣。因此,我們互相教導,而不是解決別人的問題,而不是他們:) –

+0

它返回此錯誤:SoapFault - faultcode:'SOAP-ENV:服務器'faultstring:'無法取消設置字符串偏移量'faultactor:'null'詳細信息:空值 – mahdi

1

步驟1:在KSOAP或ksoap2不直接支持發送陣列。這樣你就可以創建一個方法名SoapObject(你需要創建陣列)

SoapObject object= new SoapObject(NAMESPACE,"shoppingCartProductEntity"); 
object.addProperty("product_id","886"); 
object.addProperty("sku","ABC 456-Black-10"); 
     and more parameters..... 

步驟2:然後創建arrayType中方法(可選取決於你的WSDL),這soapObject添加到該數組對象作爲物業

SoapObject EntityArray = new SoapObject(NAMESPACE, "shoppingCartProductEntityArray"); 
EntityArray.addProperty("products",object); 

步驟3:終於陣列添加到您的主SOAP調用

SoapObject request = new SoapObject(NAMESPACE,"shoppingCartProductAdd"); 
request.addProperty("sessionId", sessionId); 
request.addProperty("quoteId", cartId); 
request.addProperty("products",EntityArray); //ADDING ARRAY HERE AS A PEOPERTY 
env.setOutputSoapObject(request); 
androidHttpTransport.call(NAMESPACE +"/shoppingCartProductAdd ", env); 
resultSoap = env.getResponse(); 

注意:步驟因您的WSDL而異,有時您可以直接添加第1步對象作爲參數,這取決於WSDL。