Android 1.6 ksoap2“RuntimeException:无法序列化:java.util.GregorianCalendar ..”,同时传递datetime参数

时间:2010-10-23 14:10:21

标签: android ksoap2

我必须使用kso​​ap2 api在android中调用.net web服务。 我的代码适用于将参数传递为String或int。但它传递Calendar对象作为参数时显示“ java.lang.RuntimeException:无法序列化:java.util.GregorianCalendar .....”。我将日期转换为字符串并将其解析为日期对象,但这并不令人担忧。

任何人帮助我。

谢谢。

4 个答案:

答案 0 :(得分:3)

传递日期的最简单方法是作为字符串,但是如果你想将它作为日期传递,那么你需要编写Marshal接口的类并注册信封。这基本上告诉KSOAP如何处理日期。你可以考虑这篇文章:

Implementing KSOAP Marshal Interface

希望这有帮助。

答案 1 :(得分:2)

首先输入以下代码来创建MarshalDate类。

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));
    }

}

//在您的客户端代码中:

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 :(得分:1)

以下对我有用:

  1. MarshalDateorg.ksoap2.serialization.MarshalDate(来自KSOAP2

  2. 方法参数:请注意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. 3.注意附加的md.register(信封);

        // 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);
        ////////////////////////////////////////
    

答案 3 :(得分:0)

您需要提供更多详细信息。通常使用日期,您需要知道格式并将其作为字符串传入和传出(好吧......最简单)。