最初に、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.