Java 使用Ksoap2将日期发送到服务器
我正在尝试向服务器发送请求,其中一个参数的类型应该是Java 使用Ksoap2将日期发送到服务器,java,android,soap,ksoap2,android-ksoap2,Java,Android,Soap,Ksoap2,Android Ksoap2,我正在尝试向服务器发送请求,其中一个参数的类型应该是date。如何设置类型日期?因为现在我的请求的类型是string 我正在发送到服务器的请求: <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
date
。如何设置类型日期?因为现在我的请求的类型是string
我正在发送到服务器的请求:
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:d="http://www.w3.org/2001/XMLSchema"
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header /><v:Body><n0:getOutlets id="o0" c:root="1" xmlns:n0="http://www.kltro.com">
<userId i:type="d:string">a24f0c23-5f36-11e4-b332-984be174a0cc</userId>
<date i:type="d:string">2016-08-25</date>
</n0:getOutlets>
</v:Body>
</v:Envelope>
通过
ksoap2android
库发送日期有点复杂ksoap2 android
库不知道序列化(或封送)类型日期。因此,您需要提供一个自定义的封送处理类
但幸运的是,ksoap2android
库已经有了一个MarshalDate
类。见其出处
您只需将该类注册到您的SoapSerializationEnvelope
查看下面的代码
private static final String NAMESPACE = "http://www.kltro.com";
private static final String METHODNAME = "getOutlets";
private static final String WSDL = "http://mapx.kashkan.org:5445/tro/ws/kltro";
private static final String SOAP_ACTION = NAMESPACE + "#kltro:" + METHODNAME;
private static String TAG = "soap_tester";
public static String callWebservice() {
String responseDump = "";
try {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// Register the Date Marshal class
new MarshalDate().register(envelope);
SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
request.addProperty("userId", "a24f0c23-5f36-11e4-b332-984be174a0cc");
// Your date
String dateStr = "2016-08-25";
// Parse the date into an object of type java.util.Date
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
// You might want to play around with the timezones
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("ru"), Locale.ENGLISH);
calendar.setTime(sdfIn.parse(dateStr));
// Clear the hour, minute and second
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date = calendar.getTime();;
// Create a property with the date object and type as java.util.Date.class
PropertyInfo dateProperty = new PropertyInfo();
dateProperty.setValue(date);
dateProperty.setName("date");
dateProperty.setType(Date.class);
// Add it to the request
request.addProperty(dateProperty);
envelope.bodyOut = request;
HttpTransportSE transport = new HttpTransportSE(WSDL);
transport.debug = true;
try {
transport.call(SOAP_ACTION, envelope);
String requestDump = transport.requestDump;
responseDump = transport.responseDump;
Log.e(TAG, requestDump);
Log.e(TAG, responseDump);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return responseDump;
}
这是requestDump
<?xml version="1.0" encoding="UTF-8"?>
<v:Envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<v:Header />
<v:Body>
<n0:getOutlets xmlns:n0="http://www.kltro.com" id="o0" c:root="1">
<userId i:type="d:string">a24f0c23-5f36-11e4-b332-984be174a0cc</userId>
<date i:type="d:dateTime">2016-08-24T00:00:00.000Z</date>
</n0:getOutlets>
</v:Body>
</v:Envelope>
a24f0c23-5f36-11e4-b332-984be174a0cc
2016-08-24T00:00:00.000Z
祝你好运:)我得到了java.lang.RuntimeException:无法序列化:2018-10-03T05:40:19.000+05:30你能告诉我同样的原因吗。
<?xml version="1.0" encoding="UTF-8"?>
<v:Envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<v:Header />
<v:Body>
<n0:getOutlets xmlns:n0="http://www.kltro.com" id="o0" c:root="1">
<userId i:type="d:string">a24f0c23-5f36-11e4-b332-984be174a0cc</userId>
<date i:type="d:dateTime">2016-08-24T00:00:00.000Z</date>
</n0:getOutlets>
</v:Body>
</v:Envelope>