4

我必须使用 ksoap2 api 在 android 中调用 .net Web 服务。我的代码在将参数作为 String 或 int 传递时运行良好。但它显示“ java.lang.RuntimeException:无法序列化:java.util.GregorianCalendar .....”同时将日历对象作为参数传递。我将日期转换为字符串并将其解析为日期对象,但它不起作用。

任何人帮助我。

谢谢你。

4

4 回答 4

3

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

实现 KSOAP Marshal 接口

希望这可以帮助。

于 2010-11-24T16:43:43.863 回答
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.
于 2011-01-15T02:32:15.390 回答
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.注意额外的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);
    ////////////////////////////////////////
于 2016-06-07T03:47:20.877 回答
0

您需要提供更多详细信息。一般来说,对于日期,您需要知道格式并将其作为字符串传入和传出(嗯..无论如何这都是最简单的)。

于 2010-11-04T20:52:57.193 回答