我想将数据从Android应用程序发送到php web服务,Web服务获取此参数:
Type Name Description
string sessionId Session ID
array customerData Array of customerCustomerEntityToCreate
我使用ksoap2库,在此代码中我传递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());
我发现这个php示例用于使用PHP代码的Web服务:
$client = new SoapClient('http://magentohost/api/v2_soap/?wsdl');
$session = $client->login('apiUser', 'apiKey');
$result = $client->customerCustomerCreate($session, array('email' => 'customer- mail@example.org', 'firstname' => 'Dough', 'lastname' => 'Deeks', 'password' => 'password', 'website_id' => 1, 'store_id' => 1, 'group_id' => 1));
var_dump ($result);
我怎么能用java做到这一点? 感谢
答案 0 :(得分:1)
第1步:在ksoap或ksoap2中没有直接支持发送Array。所以你可以创建一个带方法名称的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作为属性添加到该数组Object
SoapObject EntityArray = new SoapObject(NAMESPACE, "shoppingCartProductEntityArray");
EntityArray.addProperty("products",object);
第3步:最后将数组添加到主肥皂调用
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,有时您可以直接添加第一步对象作为参数,这取决于WSDL。
答案 1 :(得分: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());