Search code examples
androidobjectrequestksoap2ksoap

How to send object in ksoap2 request


I'm trying to pass an object to web service using ksopa2 library. I'm trying something like this:

        RangeInfo controllingArea = new RangeInfo();
        controllingArea.setLow(txtControllingAreaSearch.getText().toString());

        PropertyInfo propertyControllingArea = new PropertyInfo();
        propertyControllingArea.setType(new RangeInfo().getClass());
        propertyControllingArea.setName("IControllingarea");
        propertyControllingArea.setValue(controllingArea);
        request.addProperty(propertyControllingArea);

        PropertyInfo maxNoOfHits = new PropertyInfo();
        maxNoOfHits.setValue(txtMaxNoHitsSearch.getText().toString());
        maxNoOfHits.setName("IMaxnoofhits");
        maxNoOfHits.setType(PropertyInfo.INTEGER_CLASS);

        request.addProperty(maxNoOfHits);

My RangeInfo class is like this:

  public String sign = "I";
public String option = "EQ";
public String low;
public String high;

public RangeInfo(){}

public RangeInfo(SoapObject soapObject)
{
    if (soapObject == null)
        return;
    if (soapObject.hasProperty("Sign"))
    {
        Object obj = soapObject.getProperty("Sign");
        if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
            SoapPrimitive j =(SoapPrimitive) obj;
            sign = j.toString();
        }else if (obj!= null && obj instanceof String){
            sign = (String) obj;
        }
    }
    if (soapObject.hasProperty("Option"))
    {
        Object obj = soapObject.getProperty("Option");
        if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
            SoapPrimitive j =(SoapPrimitive) obj;
            option = j.toString();
        }else if (obj!= null && obj instanceof String){
            option = (String) obj;
        }
    }
    if (soapObject.hasProperty("Low"))
    {
        Object obj = soapObject.getProperty("Low");
        if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
            SoapPrimitive j =(SoapPrimitive) obj;
            low = j.toString();
        }else if (obj!= null && obj instanceof String){
            low = (String) obj;
        }
    }
    if (soapObject.hasProperty("High"))
    {
        Object obj = soapObject.getProperty("High");
        if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
            SoapPrimitive j =(SoapPrimitive) obj;
            high = j.toString();
        }else if (obj!= null && obj instanceof String){
            high = (String) obj;
        }
    }
}
@Override
public Object getProperty(int arg0) {
    switch(arg0){
        case 0:
            return sign;
        case 1:
            return option;
        case 2:
            return low;
        case 3:
            return high;
    }
    return null;
}

@Override
public int getPropertyCount() {
    return 4;
}

@Override
public void getPropertyInfo(int index, @SuppressWarnings("rawtypes") Hashtable arg1, PropertyInfo info) {
    switch(index){
        case 0:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "Sign";
            break;
        case 1:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "Option";
            break;
        case 2:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "Low";
            break;
        case 3:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "High";
            break;
    }
}

@Override
public void setProperty(int arg0, Object arg1) {
}

public void setSign(String sign) {
    this.sign = sign;
}

public void setOption(String option) {
    this.option = option;
}

public void setLow(String low) {
    this.low = low;
}

public void setHigh(String high) {
    this.high = high;
}

In the server side I'm getting value of "IMaxnoofhits" variable, but "IControllingarea" is always null!!!

Someone has passed or imagine what could be the problem? Thanks!


Solution

  • Thanks guys,

    I solved it by my self. Here is what i did. And it is working to send complex object to the request.

                    RangeInfo controllingArea = new RangeInfo();
                    controllingArea.setLow("Value");
                    controllingArea.setHigh("Value");
    
                    PropertyInfo item = new PropertyInfo();
                    item.setType(controllingArea.getClass());
                    item.setName("item");
                    item.setValue(controllingArea);
    
                    SoapObject object1 = new SoapObject("","IControllingarea");
                    object1.addProperty(item);
                    request.addSoapObject(object1);
    
    
                    PropertyInfo maxNoOfHits = new PropertyInfo();
                    maxNoOfHits.setValue(txtMaxNoHitsSearch.getText().toString());
                    maxNoOfHits.setName("IMaxnoofhits");
                    maxNoOfHits.setType(PropertyInfo.INTEGER_CLASS);
    
                    request.addProperty(maxNoOfHits);
    

    Best regards.