Cannot serialize dates with ksoap2

2019-06-25 23:45发布

No matter what format I try, I can't send a date from my Android (Java) app to the .NET web service. I've tried sending it as a GregorianCalendar, a Date, a Calendar... Nothing works.

I get a "cannot be serialized" error as a runtime exception. I'm using ksoap2 to manage my web service calls. Any ideas?

Errors below:

java.lang.RuntimeException: Cannot serialize: java.util.GregorianCalendar...
java.lang.RuntimeException: Cannot serialize: java.util.Date...

1条回答
干净又极端
2楼-- · 2019-06-26 00:38

Managed to track this down eventually through several dead ends. It seems the ksoap library doesn't know what to do with dates or doubles to be sent through on the webservice call - you need to tell it how to handle them or it gets a serialization error.

The way to do this is to implement the MarshalDate class: this tutorial has the code you need to implement it.

From my experiences, ksoap2 has the MarshalDate class already in the library and that class will do the job, but doesn't register it against the envelope by default. By registering it against the envelope (as below), the problem was resolved. For doubles or other floating point numbers, use MarshalFloat instead.

To fix this problem, before you send your SoapSerializationEnvelope through your HttpTransport make this call:

new MarshalDate().register(envelope);

where envelope is your SoapSerializationEnvelope.

I found out the ksoap2 library has the MarshalDate and MarshallFloat classes by accident when importing the class, as Eclipse provided me with two options for importing it - one of which was within ksoap2. The included class sufficed and I removed my custom one.

查看更多
登录 后发表回答