Cannot serialize dates with ksoap2

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

I get a "cannot be serialized" error as an exception at runtime. I am using ksoap2 to manage web service calls. Any ideas?

Errors below:

 java.lang.RuntimeException: Cannot serialize: java.util.GregorianCalendar... java.lang.RuntimeException: Cannot serialize: java.util.Date... 
+4
source share
1 answer

Cleverly track this in the long run through a few dead ends. The ksoap library does not seem to know what to do with dates or doubles that should be sent on a webservice call - you need to tell how to handle them, or a serialization error occurs.

The way to do this is to implement the MarshalDate class: this tutorial contains the code needed to implement it.

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

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

 new MarshalDate().register(envelope); 

where envelope is your SoapSerializationEnvelope .

I found that when loading the class, the ksoap2 library accidentally had the MarshalDate and MarshallFloat classes, because Eclipse provided me with two import options - one of which was within ksoap2. The included class was enough, and I deleted my own.

+6
source

Source: https://habr.com/ru/post/1433567/


All Articles