Android ksoap2发送复杂对象

时间:2012-07-23 22:13:37

标签: android serialization soap ksoap2 ksoap

我正在尝试通过android应用程序中的soap服务调用发送一个复杂的对象。我得到了一个非常奇怪的错误。

我尝试序列化和发送的类(以及包含在其中的类)如下:

public class TSavedMessage extends BaseObject{
public static Class TSAVEDMESSAGE_CLASS = new TSavedMessage().getClass();
protected String sToken;
protected String dateDeferredDelivery;
protected int idDepartment;
protected boolean bDraft;
protected String txtRelationsType;
protected String txtRelatedIds;
protected int idForwardOf;
protected int idReplyOf;
protected String numPriority;
protected boolean bReadReceipt;
protected boolean bDeliveryReport;
protected String txtKeywords;
protected int numTimeOffset;
protected String txtTimeZone;
protected String txtSubject;
protected String txtBody;
protected TRecipient[] recipients;

public TSavedMessage() {
}

public TSavedMessage(String sToken, String dateDeferredDelivery, int idDepartment, boolean bDraft, String txtRelationsType, String txtRelatedIds, int idForwardOf, int idReplyOf, String numPriority, boolean bReadReceipt, boolean bDeliveryReport, String txtKeywords, int numTimeOffset, String txtTimeZone, String txtSubject, String txtBody, TRecipient[] recipients, int[] vesselRelations) {
    this.sToken = sToken;
    this.dateDeferredDelivery = dateDeferredDelivery;
    this.idDepartment = idDepartment;
    this.bDraft = bDraft;
    this.txtRelationsType = txtRelationsType;
    this.txtRelatedIds = txtRelatedIds;
    this.idForwardOf = idForwardOf;
    this.idReplyOf = idReplyOf;
    this.numPriority = numPriority;
    this.bReadReceipt = bReadReceipt;
    this.bDeliveryReport = bDeliveryReport;
    this.txtKeywords = txtKeywords;
    this.numTimeOffset = numTimeOffset;
    this.txtTimeZone = txtTimeZone;
    this.txtSubject = txtSubject;
    this.txtBody = txtBody;
    this.recipients = recipients;
}

public String getSToken() {
    return sToken;
}

public void setSToken(String sToken) {
    this.sToken = sToken;
}

public String getDateDeferredDelivery() {
    return dateDeferredDelivery;
}

public void setDateDeferredDelivery(String dateDeferredDelivery) {
    this.dateDeferredDelivery = dateDeferredDelivery;
}

public int getIdDepartment() {
    return idDepartment;
}

public void setIdDepartment(int idDepartment) {
    this.idDepartment = idDepartment;
}

public boolean isBDraft() {
    return bDraft;
}

public void setBDraft(boolean bDraft) {
    this.bDraft = bDraft;
}

public String getTxtRelationsType() {
    return txtRelationsType;
}

public void setTxtRelationsType(String txtRelationsType) {
    this.txtRelationsType = txtRelationsType;
}

public String getTxtRelatedIds() {
    return txtRelatedIds;
}

public void setTxtRelatedIds(String txtRelatedIds) {
    this.txtRelatedIds = txtRelatedIds;
}

public int getIdForwardOf() {
    return idForwardOf;
}

public void setIdForwardOf(int idForwardOf) {
    this.idForwardOf = idForwardOf;
}

public int getIdReplyOf() {
    return idReplyOf;
}

public void setIdReplyOf(int idReplyOf) {
    this.idReplyOf = idReplyOf;
}

public String getNumPriority() {
    return numPriority;
}

public void setNumPriority(String numPriority) {
    this.numPriority = numPriority;
}

public boolean isBReadReceipt() {
    return bReadReceipt;
}

public void setBReadReceipt(boolean bReadReceipt) {
    this.bReadReceipt = bReadReceipt;
}

public boolean isBDeliveryReport() {
    return bDeliveryReport;
}

public void setBDeliveryReport(boolean bDeliveryReport) {
    this.bDeliveryReport = bDeliveryReport;
}

public String getTxtKeywords() {
    return txtKeywords;
}

public void setTxtKeywords(String txtKeywords) {
    this.txtKeywords = txtKeywords;
}

public int getNumTimeOffset() {
    return numTimeOffset;
}

public void setNumTimeOffset(int numTimeOffset) {
    this.numTimeOffset = numTimeOffset;
}

public String getTxtTimeZone() {
    return txtTimeZone;
}

public void setTxtTimeZone(String txtTimeZone) {
    this.txtTimeZone = txtTimeZone;
}

public String getTxtSubject() {
    return txtSubject;
}

public void setTxtSubject(String txtSubject) {
    this.txtSubject = txtSubject;
}

public String getTxtBody() {
    return txtBody;
}

public void setTxtBody(String txtBody) {
    this.txtBody = txtBody;
}

public TRecipient[] getRecipients() {
    return recipients;
}

public void setRecipients(TRecipient[] recipients) {
    this.recipients = recipients;
}

public Object getProperty(int index) {
    switch (index) {
    case 0:
        return sToken;
    case 1: 
        return dateDeferredDelivery;
    case 2:
        Integer iDepartment = new Integer(idDepartment);
        return iDepartment;
    case 3:
        Boolean isDraft = new Boolean(bDraft);
        return isDraft;
    case 4:
        return txtRelationsType;
    case 5:
        return txtRelatedIds;
    case 6:
        Integer iForwardOf = new Integer(idForwardOf);
        return iForwardOf;
    case 7:
        Integer iReplyOf = new Integer(idReplyOf);
        return iReplyOf;
    case 8:
        return numPriority;
    case 9:
        Boolean isReadReceipt = new Boolean(bReadReceipt);
        return isReadReceipt;
    case 10:
        Boolean isDeliveryReport = new Boolean(bDeliveryReport);
        return isDeliveryReport;
    case 11:
        return txtKeywords;
    case 12:
        Integer iTimeOffset = new Integer(numTimeOffset);
        return iTimeOffset;
    case 14:
        return txtTimeZone;
    case 15:
        return txtSubject;
    case 16:
        return txtBody;
    case 17:
        return recipients; // complex type
    default:
        return null;
    }
}

public int getPropertyCount() {
    return 18;
}

public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
    switch (index) {
    case 0:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "sToken";
        break;
    case 1: 
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "dateDeferredDelivery";
        break;
    case 2:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "idDepartment";
        break;
    case 3:
        info.type = PropertyInfo.BOOLEAN_CLASS;
        info.name = "bDraft";
        break;
    case 4:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "txtRelationsType";
        break;
    case 5:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "txtRelatedIds";
        break;
    case 6:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "idForwardOf";
        break;
    case 7:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "idReplyOf";
        break;
    case 8:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "numPriority";
        break;
    case 9:
        info.type = PropertyInfo.BOOLEAN_CLASS;
        info.name = "bReadReceipt";
        break;
    case 10:
        info.type = PropertyInfo.BOOLEAN_CLASS;
        info.name = "bDeliveryReport";
        break;
    case 11:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "txtKeywords";
        break;
    case 12:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "numTimeOffset";
        break;
    case 14:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "txtTimeZone";
        break;
    case 15:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "txtSubject";
        break;
    case 16:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "txtBody";
        break;
    case 17:
        info.type = PropertyInfo.VECTOR_CLASS;
        info.name = "recipients"; // complex type
        break;
    default:
        break;
    }       
}

public void setProperty(int index, Object value) {
    switch (index) {
    case 0:
        sToken = value.toString();
        break;
    case 1: 
        dateDeferredDelivery = value.toString();
        break;
    case 2:
        idDepartment = Integer.parseInt(value.toString());
        break;
    case 3:
        if(value.toString().equalsIgnoreCase("false")) bDraft = false;
        else bDraft = true;
        break;
    case 4:
        txtRelationsType = value.toString();
        break;
    case 5:
        txtRelatedIds = value.toString();
        break;
    case 6:
        idForwardOf = Integer.parseInt(value.toString());
        break;
    case 7:
        idReplyOf = Integer.parseInt(value.toString());
        break;
    case 8:
        numPriority = value.toString();
        break;
    case 9:
        if(value.toString().equalsIgnoreCase("false")) bReadReceipt = false;
        else bReadReceipt = true;
        break;
    case 10:
        if(value.toString().equalsIgnoreCase("false")) bDeliveryReport = false;
        else bDeliveryReport = true;
        break;
    case 11:
        txtKeywords = value.toString();
        break;
    case 12:
        numTimeOffset = Integer.parseInt(value.toString());
        break;
    case 14:
        txtTimeZone = value.toString();
        break;
    case 15:
        txtSubject = value.toString();
        break;
    case 16:
        txtBody = value.toString();
        break;
    case 17:
        recipients = (TRecipient[]) value; // complex type
        break;
    default:
        break;
    }
}

}

public class TRecipient extends BaseObject{
public static Class TRECIPIENT_CLASS = new TRecipient().getClass();
protected int idRecipient;
protected String txtRealAddress;
protected String Visibility;

public TRecipient() {
}

public TRecipient(int idRecipient, String txtRealAddress, String visibility) {
    this.idRecipient = idRecipient;
    this.txtRealAddress = txtRealAddress;
    this.Visibility = visibility;
}

public int getIdRecipient() {
    return idRecipient;
}

public void setIdRecipient(int idRecipient) {
    this.idRecipient = idRecipient;
}

public String getTxtRealAddress() {
    return txtRealAddress;
}

public void setTxtRealAddress(String txtRealAddress) {
    this.txtRealAddress = txtRealAddress;
}

public String getVisibility() {
    return Visibility;
}

public void setVisibility(String visibility) {
    this.Visibility = visibility;
}

public Object getProperty(int index) {
    switch (index) {
    case 0:
        Integer iRecipient = new Integer(idRecipient);
        return iRecipient;
    case 1: 
        return txtRealAddress;
    case 2:
        return Visibility;
    default:
        return null;
    }
}

public int getPropertyCount() {
    return 3;
}

public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
    switch (index) {
    case 0:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "idRecipient";
        break;
    case 1:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "txtRealAddress";
        break;
    case 2:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "Visibility";
        break;
    default:
        break;
    }
}

public void setProperty(int index, Object value) {
    switch (index) {
    case 0:
        idRecipient = Integer.parseInt(value.toString());
        break;
    case 1: 
        txtRealAddress = value.toString();
        break;
    case 2:
        Visibility = value.toString();
        break;
    default:
        break;
    }
}

}

发送请求的方法如下:

            TSavedMessage msgTemp = new TSavedMessage();
        msgTemp.setSToken(LoginActivity.sToken);
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        msgTemp.setDateDeferredDelivery(dateFormat.format(date));
        msgTemp.setIdDepartment(datasource.getDepartmentID(departmentString));
        msgTemp.setBDraft(false);
        msgTemp.setTxtRelationsType("");
        msgTemp.setTxtRelatedIds("");
        msgTemp.setIdForwardOf(params[1]);
        msgTemp.setIdReplyOf(params[2]);
        msgTemp.setNumPriority("Normal");
        msgTemp.setBReadReceipt(false);
        msgTemp.setBDeliveryReport(false);
        msgTemp.setTxtKeywords(keywords.getText().toString());
        msgTemp.setNumTimeOffset(0);
        msgTemp.setTxtTimeZone("0");
        msgTemp.setTxtSubject(subject.getText().toString());
        msgTemp.setTxtBody(body.getText().toString());
        TRecipient[] recarrayTemp = new TRecipient[1];
        recarrayTemp[0] = new TRecipient(0, "nickvoulgaris@hotmail.com", "To");
        msgTemp.setRecipients(recarrayTemp);

        SoapObject recipients = new SoapObject(LoginActivity.NAMESPACE, "Recipients");
        SoapObject recarray[] = new SoapObject[1];
        for (int i = 0; i < 1; i++){
            recarray[i] = new SoapObject(BaseObject.NAMESPACE, "TRecipient");
            recarray[i].addProperty("idRecipient", Integer.toString(0));
            recarray[i].addProperty("txtRealAddress", "nickvoulgaris@hotmail.com");
            recarray[i].addProperty("Visibility", "To");
            recipients.addProperty("TRecipient", recarray[i]);
        }

        SoapObject msg = new SoapObject(BaseObject.NAMESPACE, "MessageToSend");
        msg.addProperty("sToken", LoginActivity.sToken);
        msg.addProperty("dateDeferredDelivery", msgTemp.getDateDeferredDelivery());
        msg.addProperty("idDepartment", Integer.toString(msgTemp.getIdDepartment()));
        msg.addProperty("bDraft", "false");
        msg.addProperty("txtRelationsType", Integer.toString(0));
        msg.addProperty("txtRelatedIds", Integer.toString(0));
        msg.addProperty("idForwardOf", Integer.toString(0));
        msg.addProperty("idReplyOf", Integer.toString(0));
        msg.addProperty("numPriority", msgTemp.getNumPriority());
        msg.addProperty("bReadReceipt", "false");
        msg.addProperty("bDeliveryReport", "false");         
        msg.addProperty("txtKeywords", msgTemp.getTxtKeywords());
        msg.addProperty("numTimeOffset", msgTemp.getNumTimeOffset());
        msg.addProperty("txtTimeZone", msgTemp.getTxtTimeZone());
        msg.addProperty("txtSubject", msgTemp.getTxtSubject());
        msg.addProperty("txtBody", msgTemp.getTxtBody());
        msg.addProperty("Recipients", recipients);

        SoapObject rpc = new SoapObject(BaseObject.NAMESPACE, METHOD_NAME);       
        rpc.addProperty("sToken", LoginActivity.sToken);
        rpc.addProperty("MessageToSend", msg);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = rpc;
        envelope.dotNet = true;
        envelope.encodingStyle = SoapSerializationEnvelope.XSD;

        try {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(LoginActivity.URL); 
            androidHttpTransport.debug = true;
            androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            androidHttpTransport.call(SOAP_ACTION, envelope);
            System.out.println("=== " + envelope.getResponse().toString());                 
        }
        catch(XmlPullParserException e) {
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }

最后,我得到的错误是这个:

    SoapFault - faultcode: 'EConvertError' faultstring: '''0nickvoulgaris@hotmail.comTo'' is not a valid integer value' 

faultactor:'null'detail:null

非常不幸的是,这个错误让我无处可去。我无法理解为什么试图将这3个字段作为整数传递。另外让我感到困扰的是,左手边有三重引号,右手边有双引号。

任何想法都可能证明是非常有价值的。提前谢谢。

此外,此代码生成并尝试发送的soap对象似乎完全正常。它是以下内容:

MobSaveMessage{
sToken=MOB-2704FB8FFBC946469408A3BEE90CA163; 
MessageToSend=MessageToSend{
    sToken=MOB-2704FB8FFBC946469408A3BEE90CA163; 
    dateDeferredDelivery=2012/07/25 13:30:36; 
    idDepartment=18; 
    bDraft=false; 
    txtRelationsType=0; 
    txtRelatedIds=0; 
    idForwardOf=0; 
    idReplyOf=0; 
    numPriority=Normal; 
    bReadReceipt=false; 
    bDeliveryReport=false; 
    txtKeywords=key; 
    numTimeOffset=0; 
    txtTimeZone=0; 
    txtSubject=sub; 
    txtBody=mail;
    Recipients=Recipients{
        TRecipient=TRecipient{
            idRecipient=0; 
            txtRealAddress=nickvoulgaris@hotmail.com; 
            Visibility=To; 
        }; 
    }; 
}; 

}

1 个答案:

答案 0 :(得分:2)

毕竟事实证明我们发送给我们的WSDL文件的错误版本以及规范。如果我让任何人陷入困境,我真的很抱歉。不是我的错。我只是回应以防万一有人碰到这个问题。