KSoap2 and KvmSerializable-Send class objects with a list in webservice in java android

I am developing an Android application and trying to call a web service and pass a class object as a parameter to a method. There is a Partner list box in this class, and I think this field has never been sent to the web service.

this is my Customer class:

package com.testcustomer22; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import org.ksoap2.serialization.KvmSerializable; import org.ksoap2.serialization.PropertyInfo; public class Customer extends MainActivity implements KvmSerializable { private static final long serialVersionUID = 1L; public String Code; public String Name; public String VATNo; public String PriceList; public List<Partner> Partner = new ArrayList<Partner>(); public Customer(){} public Customer(String cCode, String cName, String cVATNo, String cPriceList, List<Partner> cPartner) { Code = cCode; Name = cName; VATNo = cVATNo; PriceList = cPriceList; Partner = cPartner; } public Object getProperty(int index) { Object res = null; switch(index) { case 0: res = this.Code; break; case 1: res = this.Name; break; case 2: res = this.VATNo; break; case 3: res = this.PriceList; break; case 4: res = this.Partner; break; } return res; } public int getPropertyCount() { return 4; } public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { switch (index) { case 0: info.type = PropertyInfo.STRING_CLASS; info.name = "Code"; break; case 1: info.type = PropertyInfo.STRING_CLASS; info.name = "Name"; break; case 2: info.type = PropertyInfo.STRING_CLASS; info.name = "VATNo"; break; case 3: info.type = PropertyInfo.STRING_CLASS; info.name = "PriceList"; break; case 4: info.type = PropertyInfo.VECTOR_CLASS; info.name = "Partner"; break; } } @SuppressWarnings("unchecked") public void setProperty(int index, Object value) { switch(index) { case 0: this.Code = value.toString(); break; case 1: this.Name = value.toString(); break; case 2: this.VATNo = value.toString(); break; case 3: this.PriceList = value.toString(); break; case 4: this.Partner = (List<com.testcustomer22.Partner>) value; break; default: break; } } } 

And this is my MainActivity, where I call webservice:

 package com.testcustomer22; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.PropertyInfo; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private static String SOAP_ACTION1 = "http://tempuri.org/Service/GetCustomer"; private static String NAMESPACE = "http://tempuri.org/"; private static String METHOD_NAME1 = "GetCustomer"; private static String URL = "http://xxxxxxxx:8000/Service?wsdl"; TextView TextView01; EditText editText2,editText1; Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView01 = (TextView)findViewById(R.id.TextView01); editText2 = (EditText)findViewById(R.id.editText2); editText1 = (EditText)findViewById(R.id.editText1); Customer C = new Customer(); C.setProperty(0,"0000000000000000"); //Initialize soap request + add parameters SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1); //Declare the version of the SOAP request SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); //Pass value for userName variable of the web service PropertyInfo pi =new PropertyInfo(); pi.setName("customer"); //Define the variable name pi.setValue(C); //set value for userName variable pi.setType(C.getClass()); //Define the type of the variable request.addProperty(pi); //Pass properties to the variable envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.debug = true; envelope.dotNet = true; envelope.addMapping(NAMESPACE, "customer", new Customer().getClass()); try{ editText2.setText("before call websrv"); //this is the actual part that will call the webservice androidHttpTransport.call(SOAP_ACTION1, envelope); //Get the SoapResult from the envelope body. SoapObject response = (SoapObject) envelope.bodyIn; Log.i("REQUEST--->", androidHttpTransport.requestDump); Log.i("RESPONSE--->", androidHttpTransport.responseDump); Log.i("Response.toString()---------->", response.toString()); }catch (Exception e) { e.printStackTrace(); } } }); } } 

I add the line "Log.i" ("REQUEST --->" ... "to print the request sent by the service. The problem is that when I write" return 5 "to" getPropertyCount () "of my Customer class and try to run my main action, it throws me an exception. And when I write "return4" in "getPropertyCount ()", it returns me this:

 I/REQUEST---------------------->(1156): <v:Envelope xmlns:i="http://www.w3.org/1999/XMLSchema-instance" xmlns:d="http://www.w3.org/1999/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><GetCustomer xmlns="http://tempuri.org/" id="o0" c:root="1"><customer i:type="d:anyType"><Code i:type="d:string">0000000000000000</Code><Name i:null="true" /><VATNo i:null="true" /> <PriceList i:null="true" /></customer></GetCustomer></v:Body></v:Envelope> 

Shows all other fields except Partner.

If you have ideas on how to pass the list box, please help me.

Many thanks.

+4
source share

Source: https://habr.com/ru/post/1494054/


All Articles