Reputation: 4863
I'm trying to send request to the server, and one of the argument there should to have type date
. How to set type date? Because right now my request has type string
.
Request which I'm sending to the server:
<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>
Activity for the sending request:
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();
}
}
Upvotes: 1
Views: 834
Reputation: 6828
Sending a date through ksoap2-android
library is a bit more complicated. ksoap2-android
library does not know to serialize(or marshal) the type date. So you need to give a custom Marshal
class.
But luckily ksoap2-android
library already has a MarshalDate
class. See its source here.
All you need to do is register this class to your SoapSerializationEnvelope.
Check out the code below,
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;
}
This is the 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>
Good luck :)
Upvotes: 1