我正在为Microsoft Dynamics NAV开发Android客户端。我正在使用ksoap2与NAV webservices进行通信。我使用MarshalDate类来序列化日期。当我将Date传递给webservice时,我收到类似&#34的错误;字符串未被识别为有效的DateTime。"
MarshalDate.java
public class MarshalDate implements Marshal {
public static Class DATE_CLASS = new Date().getClass();
public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo expected) throws IOException, XmlPullParserException {
return IsoDate.stringToDate(parser.nextText(), IsoDate.DATE_TIME);
}
public void register(SoapSerializationEnvelope cm) {
cm.addMapping(cm.xsd, "dateTime", Date.class, this); }
public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
writer.text(IsoDate.dateToString((Date) obj, IsoDate.DATE_TIME)); } }
我的salesordercard对象,
SalesOrderCard.java
public class SalesOrderCard implements KvmSerializable
{
String Key;
String No;
String Sell_to_Customer_No;
Sales_Order_Line_List Salesorderlinelist;
String ShopNo;
Date OrderDate;
double Received_Amount;
boolean Cheque;
public SalesOrderCard(){}
public SalesOrderCard(String key,String no,String selltocustno,Sales_Order_Line_List salesorderline,String shopno,Date orderdate ,double received_Amount,boolean cheque) {
Key = key;
No = no;
Sell_to_Customer_No = selltocustno;
Salesorderlinelist=salesorderline;
ShopNo=shopno;
OrderDate=orderdate;
received_Amount=Received_Amount;
Cheque=cheque;
}
public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return Key;
case 1:
return No;
case 2:
return Sell_to_Customer_No;
case 3:
return Salesorderlinelist;
case 4:
return ShopNo;
case 5:
return Received_Amount;
case 6:
return Cheque;
case 7:
return OrderDate;
}
return null;
}
public int getPropertyCount() {
return 8;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch(index)
{
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Key";
break;
case 1:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "No";
break;
case 2:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Sell_to_Customer_No";
break;
case 3:
info.type = Sales_Order_Line_List.class;
info.name = "SalesLines";
break;
case 4:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Shop_No";
break;
case 5:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Received_Amount";
break;
case 6:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Cheque";
break;
case 7:
info.type = MarshalDate2.DATE_CLASS;
info.name = "Order_Date";
break;
default:break;
}
}
public void setProperty(int index, Object value) {
switch(index)
{
case 0:
Key = value.toString();
break;
case 1:
No = value.toString();
break;
case 2:
Sell_to_Customer_No = value.toString();
break;
case 3:
Salesorderlinelist = (Sales_Order_Line_List)value;
break;
case 4:
ShopNo = value.toString();
break;
case 5:
Received_Amount = Double.parseDouble(value.toString());
break;
case 6:
Cheque = Boolean.parseBoolean(value.toString());
break;
case 7:
OrderDate=(Date)value;
break;
default:
break;
}
}
}
我正在传递约会,
String DateString="22-05-2014 19:32:52";
SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
d=(Date)format.parse(DateString);
SalesOrderCard cr=new SalesOrderCard();
cr.OrderDate=d;
PropertyInfo custProp = new PropertyInfo();
custProp.setName("SalesOrderCard");
custProp.setValue(cr);
custProp.setType(SalesOrderCard.class);
request.addProperty(custProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
MarshalDouble md = new MarshalDouble();
md.register(envelope);
MarshalDate mdate=new MarshalDate();
mdate.register(envelope);
答案 0 :(得分:1)
假设d是日期,则此代码看起来正确
String DateString="22-05-2014 19:32:52";
SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
d=(Date)format.parse(DateString);
我怀疑这一行可能是问题
writer.text(IsoDate.dateToString((Date) obj, IsoDate.DATE_TIME));
如果您知道writer.text期望的格式,那么我会尝试使用SimpleDateFormat为您创建字符串:
String dateString = format.format((Date)obj);