KSOAP2 - 在另一个复杂对象中序列化复杂对象数组

时间:2013-04-09 11:30:32

标签: android marshalling ksoap2 soap-serialization

我正在尝试通过ksoap2将复杂对象发送到webmethod。当IDE执行AndroidHttpTransport.call命令时会发生问题,它会发送该错误;

  

java.lang.RuntimeException:无法序列化:[webServiceClasses.movementItem@4123d840]

我将参数发送到下面的webmethod;

[WebMethod]
        public TransferReturn ApplyVanToVanMovement(vanToVanMovement vMovement)
        {
            OracleConnection opCnn = new       OracleConnection(Settings.Instance.GetConnectionString());
            opCnn.Open();
            DBOperations dbOp = new DBOperations();

         .
         .
         .
        }

这是我将paramateres发送给webmethod的代码;

public static TransferReturn ApplyVanToVanMovement(TransferReturn pTransferReturn, vanToVanMovement vMovement)
        throws Exception {

    String NAMESPACE = "http://tempuri.org/";
    String METHOD_NAME = "ApplyVanToVanMovement";
    String SOAP_ACTION = "http://tempuri.org/ApplyVanToVanMovement";
    String URL;
    if (Settings.instance().getDocNum().isApn == 0)
        URL = Settings.instance().getDocNum().WANIP + Settings.instance().getDocNum().WANTransferServiceUrl;
    else
        URL = Settings.instance().getDocNum().LANIP + Settings.instance().getDocNum().LANTransferServiceUrl;


    PropertyInfo pi = new PropertyInfo();
    pi.setName("vanToVanMovement");
    pi.setValue(vMovement);
    pi.setType(vMovement.getClass());
    Request.addProperty(pi);

    ArrayList<movementItem> alMi = vMovement.getItemCollectionField();

    for (int i = 0; i < alMi.size(); i++) {
        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("movementItem" + i);
        pi2.setValue(alMi.get(i));
        pi2.setType(movementItem.class);
        Request.addProperty(pi2);
    }

    /*
     * Set the web service envelope
     */
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(Request);

    envelope.addMapping(NAMESPACE, "vanToVanMovement", new vanToVanMovement().getClass());

    for (int i = 0; i < alMi.size(); i++) {
        envelope.addMapping(NAMESPACE, "movementItem" + i, movementItem.class);
    }

    MarshalDouble marshaldDouble = new MarshalDouble();
    marshaldDouble.register(envelope);

    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

    /*
     * Call the web service and retrieve result ... how luvly <3
     */SoapObject response = null;
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        response = (SoapObject) envelope.getResponse();

    } catch (Exception e) {
        e.printStackTrace();
    }
    // (SoapObject) wsClass.InvokeMethodMarshal(pInfo);

    pTransferReturn = (TransferReturn) DBLayout.setSoapObjectToClass(response, pTransferReturn);
    return pTransferReturn;
}

我的类,第一个是我发送的参数,第二个是第一个属性作为数组(我用Vector而不是ArrayList尝试相同的东西,没有改变。);

public class vanToVanMovement implements WebServiceReturn {
private long companyIdField;

private long branchIdField;

private long createdUserIdField;

private long fromWarehouseIdField;

private long toWarehouseIdField;

private long representativeIdField;

private long routeIdField;

private long vehicleIdField;

private ArrayList<movementItem> itemCollectionField;

public vanToVanMovement() {

}



public long getCompanyIdField() {
    return companyIdField;
}

public void setCompanyIdField(long companyIdField) {
    this.companyIdField = companyIdField;
}

public long getBranchIdField() {
    return branchIdField;
}

public void setBranchIdField(long branchIdField) {
    this.branchIdField = branchIdField;
}

public long getCreatedUserIdField() {
    return createdUserIdField;
}

public void setCreatedUserIdField(long createdUserIdField) {
    this.createdUserIdField = createdUserIdField;
}

public long getFromWarehouseIdField() {
    return fromWarehouseIdField;
}

public void setFromWarehouseIdField(long fromWarehouseIdField) {
    this.fromWarehouseIdField = fromWarehouseIdField;
}

public long getToWarehouseIdField() {
    return toWarehouseIdField;
}

public void setToWarehouseIdField(long toWarehouseIdField) {
    this.toWarehouseIdField = toWarehouseIdField;
}

public long getRepresentativeIdField() {
    return representativeIdField;
}

public void setRepresentativeIdField(long representativeIdField) {
    this.representativeIdField = representativeIdField;
}

public long getRouteIdField() {
    return routeIdField;
}

public void setRouteIdField(long routeIdField) {
    this.routeIdField = routeIdField;
}

public long getVehicleIdField() {
    return vehicleIdField;
}

public void setVehicleIdField(long vehicleIdField) {
    this.vehicleIdField = vehicleIdField;
}

public ArrayList<movementItem> getItemCollectionField() {
    return itemCollectionField;
}

public void setItemCollectionField(ArrayList<movementItem> itemCollectionField) {
    this.itemCollectionField = itemCollectionField;
}

public Object getProperty(int index) {
    switch (index) {
    case 0:
        return companyIdField;
    case 1:
        return branchIdField;
    case 2:
        return createdUserIdField;
    case 3:
        return fromWarehouseIdField;
    case 4:
        return toWarehouseIdField;
    case 5:
        return representativeIdField;
    case 6:
        return routeIdField;
    case 7:
        return vehicleIdField;
    case 8:
        return itemCollectionField;

    default:
        return null;
    }
}

public int getPropertyCount() {
    return 9;
}

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo pInfo) {
    switch (index) {
    case 0:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "companyIdField";
        break;
    case 1:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "branchIdField";
        break;
    case 2:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "createdUserIdField";
        break;
    case 3:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "fromWarehouseIdField";
        break;
    case 4:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "toWarehouseIdField";
        break;
    case 5:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "representativeIdField";
        break;
    case 6:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "routeIdField";
        break;
    case 7:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "vehicleIdField";
        break;
    case 8:
        pInfo.type = PropertyInfo.OBJECT_CLASS;
        pInfo.name = "itemCollectionField";
        break;

    default:
        break;
    }
}

public void setProperty(int index, Object obj) {
    switch (index) {
    case 0:
        companyIdField = Long.valueOf(obj.toString());
        break;
    case 1:
        branchIdField = Long.valueOf(obj.toString());
        break;
    case 2:
        createdUserIdField = Long.valueOf(obj.toString());
        break;
    case 3:
        fromWarehouseIdField = Long.valueOf(obj.toString());
        break;
    case 4:
        toWarehouseIdField = Long.valueOf(obj.toString());
        break;
    case 5:
        representativeIdField = Long.valueOf(obj.toString());
        break;
    case 6:
        routeIdField = Long.valueOf(obj.toString());
        break;
    case 7:
        vehicleIdField = Long.valueOf(obj.toString());
        break;
    case 8:
        itemCollectionField = (ArrayList<movementItem>) obj;
        break;

    default:
        break;
    }
}

public int getArrayNumber(String s) {

    int i = -1;

    if (s.equals("companyIdField"))
        i = 0;

    if (s.equals("branchIdField"))
        i = 1;

    if (s.equals("createdUserIdField"))
        i = 2;

    if (s.equals("fromWarehouseIdField"))
        i = 3;

    if (s.equals("toWarehouseIdField"))
        i = 4;

    if (s.equals("representativeIdField"))
        i = 5;

    if (s.equals("routeIdField"))
        i = 6;

    if (s.equals("vehicleIdField"))
        i = 7;

    if (s.equals("itemCollectionField"))
        i = 8;

    return i;
}

}

public class movementItem implements WebServiceReturn {
private long itemIdField;

private long unitSetIdField;

private long unitIdField;

private long unitSetId2Field;

private long unitId2Field;

private double convFact1Field;

private double convFact2Field;

private double planQuantityField;

private double planQuantity1Field;

private double planQuantity2Field;

public movementItem() {

}

public long getItemIdField() {
    return itemIdField;
}

public void setItemIdField(long itemIdField) {
    this.itemIdField = itemIdField;
}

public long getUnitSetIdField() {
    return unitSetIdField;
}

public void setUnitSetIdField(long unitSetIdField) {
    this.unitSetIdField = unitSetIdField;
}

public long getUnitIdField() {
    return unitIdField;
}

public void setUnitIdField(long unitIdField) {
    this.unitIdField = unitIdField;
}

public long getUnitSetId2Field() {
    return unitSetId2Field;
}

public void setUnitSetId2Field(long unitSetId2Field) {
    this.unitSetId2Field = unitSetId2Field;
}

public long getUnitId2Field() {
    return unitId2Field;
}

public void setUnitId2Field(long unitId2Field) {
    this.unitId2Field = unitId2Field;
}

public double getConvFact1Field() {
    return convFact1Field;
}

public void setConvFact1Field(double convFact1Field) {
    this.convFact1Field = convFact1Field;
}

public double getConvFact2Field() {
    return convFact2Field;
}

public void setConvFact2Field(double convFact2Field) {
    this.convFact2Field = convFact2Field;
}

public double getPlanQuantityField() {
    return planQuantityField;
}

public void setPlanQuantityField(double planQuantityField) {
    this.planQuantityField = planQuantityField;
}

public double getPlanQuantity1Field() {
    return planQuantity1Field;
}

public void setPlanQuantity1Field(double planQuantity1Field) {
    this.planQuantity1Field = planQuantity1Field;
}

public double getPlanQuantity2Field() {
    return planQuantity2Field;
}

public void setPlanQuantity2Field(double planQuantity2Field) {
    this.planQuantity2Field = planQuantity2Field;
}

public Object getProperty(int index) {
    switch (index) {

    case 0:
        return itemIdField;
    case 1:
        return unitSetIdField;
    case 2:
        return unitIdField;
    case 3:
        return unitSetIdField;
    case 4:
        return unitSetId2Field;
    case 5:
        return unitId2Field;
    case 6:
        return convFact1Field;
    case 7:
        return convFact2Field;
    case 8:
        return planQuantityField;
    case 9:
        return planQuantity1Field;

    default:
        return null;
    }
}

public int getPropertyCount() {
    return 10;
}

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo pInfo) {
    switch (index) {
    case 0:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "itemIdField";
        break;
    case 1:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "unitSetIdField";
        break;
    case 2:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "unitIdField";
        break;
    case 3:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "unitSetId2Field";
        break;
    case 4:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "unitId2Field";
        break;
    case 5:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "convFact1Field";
        break;
    case 6:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "convFact2Field";
        break;
    case 7:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "planQuantityField";
        break;
    case 8:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "planQuantity1Field";
        break;
    case 9:
        pInfo.type = PropertyInfo.LONG_CLASS;
        pInfo.name = "planQuantity2Field";
        break;

    default:
        break;
    }
}

public void setProperty(int index, Object obj) {
    switch (index) {
    case 0:
        itemIdField = Long.valueOf(obj.toString());
        break;
    case 1:
        unitSetIdField = Long.valueOf(obj.toString());
        break;
    case 2:
        unitIdField = Long.valueOf(obj.toString());
        break;
    case 3:
        unitSetId2Field = Long.valueOf(obj.toString());
        break;
    case 4:
        unitId2Field = Long.valueOf(obj.toString());
        break;
    case 5:
        convFact1Field = (double)Double.valueOf(obj.toString());
        break;
    case 6:
        convFact2Field = (double)Double.valueOf(obj.toString());
        break;
    case 7:
        planQuantityField = (double)Double.valueOf(obj.toString());
        break;
    case 8:
        planQuantity1Field = (double)Double.valueOf(obj.toString());
        break;
    case 9:
        planQuantity2Field = (double)Double.valueOf(obj.toString());
        break;

    default:
        break;
    }
}

public int getArrayNumber(String s) {

    int i = -1;

    if (s.equals("itemIdField"))
        i = 0;

    if (s.equals("unitSetIdField"))
        i = 1;

    if (s.equals("unitIdField"))
        i = 0;

    if (s.equals("unitSetId2Field"))
        i = 1;

    if (s.equals("unitId2Field"))
        i = 0;

    if (s.equals("convFact1Field"))
        i = 1;

    if (s.equals("convFact2Field"))
        i = 0;

    if (s.equals("planQuantityField"))
        i = 1;

    if (s.equals("planQuantity1Field"))
        i = 0;

    if (s.equals("planQuantity2Field"))
        i = 1;

    return i;
}

这两个类来自;

public interface WebServiceReturn extends KvmSerializable {

    public int getArrayNumber(String s);

}

最后,我的编组方法,用于序列化双打;

   public class MarshalDouble implements Marshal {
    public Object readInstance(XmlPullParser parser, String namespace, String name,
                           PropertyInfo expected) throws IOException, XmlPullParserException {
        return Double.parseDouble(parser.nextText());
    }


    public void register(SoapSerializationEnvelope cm) {
        cm.addMapping("http://tempuri.org/", "double", Double.class, this);
    }


    public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
        writer.text(obj.toString());
    }
}

0 个答案:

没有答案
相关问题