Android 1.6 ksoap2 "RuntimeException: cannot serialize: java.util.GregorianCalendar .." by passing the datetime parameter

I need to call .net web services in android using ksoap2 api. My code works well for passing a parameter as String or int. But it shows " java.lang.RuntimeException: cannot serialize: java.util.GregorianCalendar ....." when passing the Calendar object as a parameter. I converted the date to a string and parsed it into a date object, but this is not a concern.

Someone help me.

Thanks.

+4
source share
4 answers

The easiest way to pass a date is a string, but if you want to pass it as Date, you need to write a Marshall interface class and register the envelope. This basically tells KSOAP how to handle dates. You can review this post:

Implementing the Marshal KSOAP Interface

Hope this helps.

+3
source

First enter the code below to create the MarshalDate class.

package Marshals; import java.io.IOException; import java.util.Date; import org.kobjects.isodate.IsoDate; import org.ksoap2.serialization.Marshal; import org.ksoap2.serialization.PropertyInfo; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; 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 { //IsoDate.DATE_TIME=3 String Test1 = parser.nextText(); return IsoDate.stringToDate(parser.nextText(), IsoDate.DATE_TIME); } public void register(SoapSerializationEnvelope cm) { cm.addMapping(cm.xsd, "dateTime", MarshalDate.DATE_CLASS, this); // "DateTime" is wrong use "dateTime" ok } public void writeInstance(XmlSerializer writer, Object obj) throws IOException { String Test=""; Test = IsoDate.dateToString((Date) obj, IsoDate.DATE_TIME); writer.text(IsoDate.dateToString((Date) obj, IsoDate.DATE_TIME)); } } 

// In your client code:

 String result3=""; try { String soapAction3 = "http://tempuri.org/HelloWorldDate"; SoapObject rpc3 = new SoapObject(serviceNamespace, "HelloWorldDate"); PropertyInfo pi = new PropertyInfo(); pi.name= "Date"; // name of the parameter in your dotnet variable pi.type = MarshalDate.DATE_CLASS; // add property with your value, I use new Date(System.currentTimeMillis() rpc3.addProperty(pi, new Date(System.currentTimeMillis())); SoapSerializationEnvelope envelope3 = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope3.bodyOut = rpc3; envelope3.dotNet = false; MarshalDate md = new MarshalDate(); md.register(envelope3); envelope3.setOutputSoapObject(rpc3); HttpTransport ht3 = new HttpTransport(serviceUrl); ht3.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); ht3.debug = true; ht3.call(soapAction3, envelope3); result3= envelope3.getResponse().toString(); } catch(Exception ex) { //if we get an exception we'll just write the msg to the screen. result3 = ex.toString(); } don't forget envelope3.dotNet = false; it is very important otherwise you will send null date value to .net. 
+2
source

The following worked for me:

  • MarshalDate org.ksoap2.serialization.MarshalDate (from KSOAP2 )

  • Method parameter: pay attention to param2.type = MarshalDate.DATE_CLASS;

      // Your date may have time too? SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM d, yyyy"); java.util.Date date = null; try { date = formatter.parse(p_obj.LoginUser_Created_On); } catch (ParseException e) { //My handler } //// PropertyInfo wsParams2 = new PropertyInfo(); // Set Name wsParams2.setName("d_LoginUser_Created_On"); // Set Value //////////////////////////////////////// wsParams2.setValue(date);//sqlStartDate); // Set dataType //wsParams2.setType(java.sql.Date.class); // do not work //wsParams2.setType(java.util.Date.class); // do not work wsParams2.type = MarshalDate.DATE_CLASS; // works //////////////////////////////////////// // Add the property to request object request.addProperty(wsParams2); 

3. Pay attention to the additional md.register (envelope);

  // Create envelope SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); //Set envelope as dotNet envelope.dotNet = true; // Set output SOAP object envelope.setOutputSoapObject(request); //////////////////////////////////////// // MarshalDate md = new MarshalDate(); md.register(envelope); //////////////////////////////////////// 
+1
source

You need to provide more details. In general, with dates, you will need to know the format and pass it as a string (well, anyway, this is easiest).

0
source

All Articles