Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot serialize dates with ksoap2

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...
like image 719
Yoda Avatar asked Sep 11 '12 11:09

Yoda


1 Answers

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.

like image 166
Yoda Avatar answered Oct 12 '22 00:10

Yoda