Use Complex Type 'DateTime' for .Net Webservice through KSOAP Lib

There is a DateTime type parameter in .Net Webservice. The request should be like this:

...

<soap:Body>

   <TestDatetime xmlns=“...">

        <oldTime>dateTime</oldTime>

   </TestDatetime>

</soap:Body>

...


If you pass a Date to the request:

  

 soapObject.addProperty("oldTime", dateObj);

There would be "cannot be serialized.." error. If you just use "dateObj.toString()". You would get "http 500" error responses.

Google it:

1.http://stackoverflow.com/questions/4004382/android-1-6-ksoap2-runtimeexception-cannot-serialize-java-util-gregoriancalen;

This way is Implementing KSOAP Marshal Interface.

I do just as what it says:

public static String testDateTime(Date oldTime){

....

   SoapObject rpcObj = new SoapObject(NAMESPACE, METHODNAME);

    PropertyInfo parPtyInfo = new PropertyInfo();

     parPtyInfo.name = "oldTime";

     parPtyInfo.type = MarshalDate.DATE_CLASS;

      rpcObj.addPropertyIfValue(parPtyInfo, oldTime);

...

}

In the MarshalDate class which implements Marshal, use "IsoDate.dateToString((Date)oldTime, IsoDate.DATE_TIME);" to complete the "writeInstance" method.

A new error occurs. It is about TimeZone.

The timezone in each side(client and server)  are GMT+08. But the result from "writeInstance" is GMT 0.

The reason is IsoDate object.

After Seeing the IsoDate code, there would be no  smog. It use Calendar with special timezone. That is it.

At the same time. We can see the result's formation is "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", so I change my own solution:

Just use SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") to format the "Date" object.

The result is OK.

Formation codes:

public static String getFullStringForWebservice(Date oldTime){

     return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(oldTime);

}

In the web service requestion code, just do as:

rpcObj.addProperty("oldTime", getFullStringForWebservice(oldTime));

Links:

1.http://stackoverflow.com/questions/12368631/cannot-serialize-dates-with-ksoap2;

2.http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html;

3.IsoDate code: http://wsrf4j2me.googlecode.com/svn-history/r3/trunk/wsrf4j2me/ksoap-src-latest/org/kobjects/isodate/IsoDate.java;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值