Michał Belka
Michał Belka

Reputation: 21

How to send DateTime to .net webService from android (ksoap2)?

I've a .net webservice, where I have to send a param (type of dateTime), as You can see:

 <startDateTime>dateTime</startDateTime>

In Android client, I use ksoap2, and I don't know , How to send that type of data? Please help with setting this type - code below not works.

PropertyInfo propInfo3 = new PropertyInfo();
propInfo3.name="startDateTime";
propInfo3.value="2012-02-01";

Upvotes: 2

Views: 6226

Answers (2)

Tjaart
Tjaart

Reputation: 4129

Here is how I call web service methods in my application. Note the method I used to convert java dates. You require an ISO date format.

protected static Object callMethod(String method, Map<String, Object> parameters) throws IOException, XmlPullParserException {
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, method);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.headerOut = new Element[1];
    envelope.headerOut[0] = buildAuthHeader();
    envelope.setOutputSoapObject(request);
    if (parameters != null) {
        for (String item : parameters.keySet()) {
            Object itemValue = parameters.get(item);
            if (itemValue.getClass().getName().equals("java.util.Date")) {
                // If it's a date then we have to format it because ksoap
                // does not know how to do this.
                request.addProperty(item, getSOAPDateString((java.util.Date) itemValue));
            } else {
                request.addProperty(item, itemValue);
            }
        }
    }

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS, 10000);
    httpTransport.debug = true;

    httpTransport.call(WSDL_TARGET_NAMESPACE + "/" + method, envelope);
    String soapString = httpTransport.requestDump;
    System.out.println(soapString);
    return envelope.getResponse();
}

Here is the method that returns the actual string:

private static Object getSOAPDateString(java.util.Date itemValue) {
    String lFormatTemplate = "yyyy-MM-dd'T'hh:mm:ss'Z'";
    DateFormat lDateFormat = new SimpleDateFormat(lFormatTemplate);
    String lDate = lDateFormat.format(itemValue);

    return lDate;
}

Upvotes: 5

William Da Silva
William Da Silva

Reputation: 723

In your server, what's the return from the client?

When I have a problem like this, I show in the Logcat on Android what I'm sending and in the server side what I'm getting.

I had a little problem with date too (I'm using ksoap2 and webservices), I solve my problem sending the date in the util.Date, then I use SimpleDateFormat with a pattern date in my project and convert that string to what I want.

cya, Bertan


Here is my code that sends to the WS:

public static byte[] send(String... param) throws SocketTimeoutException, IOException, XmlPullParserException, Exception{
            //First I send the WS Name
    String ws = param[0];
            //Second is the operationName
    SoapObject soap = new SoapObject(URL_SOAP, param[1]);

    Object retorno = null;

    int tamParam = param.length;

            //The 3 parameter for the infinity its the properties, name and the next it's the value...
    if (tamParam > 0) {
        for (int i = 2; i < tamParam; i++) {
            soap.addProperty(param[i], param[++i]);
        }
    }

    // create a envelope for the soap object
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(soap);

    // create a HttpTransport to send the object soap with 15s delay
    MyHttpTransport httpTransport = new MyHttpTransport(URL + ws, 15000);

    // send the req
    Long tempo1 = 0l;
        tempo1 = System.currentTimeMillis();
        httpTransport.call("", envelope);

        retorno = envelope.getResponse();
        Long tempo2 = System.currentTimeMillis();

    httpTransport.reset();
    //I ever get byte[] from the WS...
    if (retorno != null) {
        byte[] bloc = Base64.decode(retorno.toString(), Base64.DEFAULT);
        return bloc;
    } else {
        return null;
    }
}

Upvotes: 0

Related Questions