我正在尝试向服务器发送请求,其中一个参数应该是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>
发送请求的活动:
public class MainActivity extends AppCompatActivity {
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";
public static String callWebservice() {
String responseDump = "";
String requestDump="";
try {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
request.addProperty("userId", "a24f0c23-5f36-11e4-b332-984be174a0cc");
request.addProperty("date", "2016-08-25");
envelope.bodyOut = request;
HttpTransportSE transport = new HttpTransportSE(WSDL);
transport.debug = true;
try {
transport.call(SOAP_ACTION, envelope);
requestDump = transport.requestDump;
responseDump = transport.responseDump;
Log.e(TAG, requestDump +"requestDump");
Log.e(TAG, responseDump+"responseDump");
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, requestDump +"requestDump");
Log.e(TAG, responseDump+"responseDump");
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, requestDump+"responseDump");
Log.e(TAG, responseDump+"responseDump");
}
Log.e("requestDump", requestDump);
Log.e(TAG, responseDump+"responseDump");
return responseDump;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
callWebservice();
}
}
答案 0 :(得分:1)
通过ksoap2-android
库发送日期有点复杂。 ksoap2-android
库不知道序列化(或编组)类型日期。因此,您需要提供自定义Marshal
类。
但幸运的是ksoap2-android
库已经有MarshalDate
类了。请参阅其来源here。
您需要做的就是将此类注册到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>
祝你好运:)