如何在ksoap2请求中发送对象

时间:2014-07-31 08:02:21

标签: android object request ksoap2 ksoap

我试图使用kso​​pa2库将对象传递给Web服务。我正在尝试这样的事情:

        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);

我的RangeInfo类是这样的:

  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;
}

在服务器端,我获得了" IMaxnoofhits"的价值。变量,但" IControllingarea"永远是空的!!!

有人通过或想象可能是什么问题? 谢谢!

2 个答案:

答案 0 :(得分:0)

在我看来,当你实例化你没有传入SoapObject的RangeInfo controllingArea = new RangeInfo();类时,你只是调用空构造函数,而不是第二个包含所有代码的构造函数。

此外,我知道这不是您问题的一部分,但您的maxNoOfHits参数设置为Integer类,即使.setValue看起来像是在传递一个字符串。

答案 1 :(得分:0)

谢谢你们,

我自己解决了。这就是我做的。它正在努力将复杂的对象发送到请求。

                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);

最好的问候。