SoFunction
Updated on 2025-04-09

Android passes complex data types and webservice published by CXF through ksoap2

Android passes complex data types and webservice published by CXF through ksoap2

I recently worked on something at school and worked on a webservice for 2 days. I was so tired. I finally figured it out with my friends at noon today. I felt that I didn’t notice some details. Ah, my time has passed like this. In order not to let my friends take detours, I should write the development document seriously!

First of all, if we want to use CXF to publish webservice with custom type objects as parameters, we should serialize this class first. The following is the code I tested. I created a TGrade class and implemented the KvmSerializable interface. There are three methods in this interface. The advantage of this interface is that the server does not need to deserialize the entity object.

public class TGrade implements KvmSerializable { 
 
  // Fields 
 
  private Integer GId; 
  private Integer GMax; 
  private Integer GMin; 
  private String GName; 
  private String GPic; 
  private String GType; 
   
  // Constructors 
  /** default constructor */ 
  public TGrade() { 
  } 
 
  /** minimal constructor */ 
  public TGrade(Integer GMax) { 
     = GMax; 
  } 
 
  /** full constructor */ 
  public TGrade(Integer GMax, Integer GMin, String GName, String GPic, 
      String GType) { 
     = GMax; 
     = GMin; 
     = GName; 
     = GPic; 
     = GType; 
  } 
 
  // Property accessors 
  public Integer getGId() { 
    return ; 
  } 
 
  public void setGId(Integer GId) { 
     = GId; 
  } 
 
  public Integer getGMax() { 
    return ; 
  } 
 
  public void setGMax(Integer GMax) { 
     = GMax; 
  } 
 
  public Integer getGMin() { 
    return ; 
  } 
 
  public void setGMin(Integer GMin) { 
     = GMin; 
  } 
 
  public String getGName() { 
    return ; 
  } 
 
  public void setGName(String GName) { 
     = GName; 
  } 
 
  public String getGPic() { 
    return ; 
  } 
 
  public void setGPic(String GPic) { 
     = GPic; 
  } 
 
  public String getGType() { 
    return ; 
  } 
 
  public void setGType(String GType) { 
     = GType; 
  } 
 
  @Override 
  public Object getProperty(int arg0) { 
    switch (arg0) {  
    case 0:  
      return GId;  
    case 1:  
      return GMax;  
    case 2:  
      return GMin;  
    case 3:  
      return GName;  
    case 4:  
      return GPic; 
    case 5:  
      return GType;  
    default:  
      break;  
    }  
    return null;  
  } 
 
  @Override 
  public int getPropertyCount() { 
    // TODO Auto-generated method stub 
    return 6;// y Please note that this must be equal to the number of parameters, otherwise the server will not be able to accept some parameters  } 
 
  @Override 
  public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) { 
    switch (arg0) { 
    case 0:  
       = PropertyInfo.STRING_CLASS;  
       = "GId";  
      break;  
    case 1:  
       = PropertyInfo.STRING_CLASS;  
       = "GMax";  
      break;  
    case 2:  
       = PropertyInfo.STRING_CLASS;  
       = "GMin";  
      break;  
    case 3:  
       = PropertyInfo.STRING_CLASS;  
       = "GName";  
      break;  
    case 4:  
       = PropertyInfo.STRING_CLASS;  
       = "GPic";  
      break;  
    case 5:  
       = PropertyInfo.STRING_CLASS;  
       = "GType";  
      break;  
    default:  
      break;  
    }     
  } 
 
  @Override 
  public void setProperty(int arg0, Object arg1) { 
    switch (arg0) { 
    case 0: 
      GId=(()); 
      break; 
    case 1: 
      GMax=(()); 
 
      break; 
    case 2: 
      GMin=(()); 
 
      break; 
    case 3: 
      GName=(); 
 
      break; 
    case 4: 
      GPic=(); 
 
      break; 
    case 5: 
 
      GType=(); 
      break; 
 
    default: 
      break; 
    } 
  } 
 
 
 
} 
 
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
public boolean addMaintenanceInfo() { 
    String methodName = "addGrade";//Server side method    String soapAction =“http://10.127.80.67/gbckf/Android/GradeService”+methodName; 
     
    TGrade person = new TGrade(); 
    (0, "6"); 
    (1, 1); 
    (3, "1"); 
    (4, "1"); 
    (5, "1"); 
    // Create a webservice connection object    HttpTransportSE transport = new HttpTransportSE(,5000);//5 seconds timeout     = true;// Is it in debug mode?    // Set connection parameters    SoapObject soapObject = new SoapObject(, methodName); 
    PropertyInfo objekt = new PropertyInfo(); 
    ("arg0");//This arg0 is very important. It cannot be anything else, it can only be arg0. Don't ask me why, otherwise you will never accept parameters because it is an xml document type thing    (person); 
    (); 
    (objekt); 
    // Set return parameters    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// Soap protocol version must use SoapEnvelope.VER11 (Soap     = false;// Note: This property is supported for the dotnetwebservice protocol, if dotnet's webservice     = transport; 
    ("Request Parameters", ()); 
    (soapObject);// Set request parameters      (, "addGrade", );// When passing an object, the parameter namespace is specified in the webservice.      (new MarshalBase64()).register(envelope); 
      try { 
      (soapAction, envelope); 
      if( instanceof SoapFault){ 
        String str = ((SoapFault) ).faultstring; 
        ("Things returned by an empty node", str); 
      }else { 
        // SoapObject sb = (SoapObject);// The object returned by the server is in the bodyIn of envelope        Object obj = ();// directly cast the return value to a known object        //("WebService", "Return result: " + ());      } 
    } 
    catch (IOException e) { 
      (); 
    } 
    catch (XmlPullParserException e) { 
      (); 
    } 
    catch (Exception ex) { 
      (); 
    } 
 
    return true; 

The above is the code I wrote by myself. If you don’t understand, please leave me a message and let me show you. Please note that the request network cannot be placed on the main thread, otherwise you will have an error.

Thank you for reading, I hope it can help you. Thank you for your support for this site!