简体   繁体   中英

Passing date to ASMX web service

I use asmx web service writes in c# over android device.

I make connection and when in some web method I need integer or string like input param,all work great, but problem is when web method need date, I try to send date in many format but always I have problem to get answer.

I need to send date object, or like string? It is possible that web service view date like something else?

This is method for "communication" with web service:

public void connectSOAP() 
{
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    String dateStr = "04/05/2010"; 
    Date dateObj=null;
    SimpleDateFormat curFormater = new SimpleDateFormat("dd/mmm/yyyy"); 
    try 
    {
        dateObj = curFormater.parse(dateStr);
    } 
    catch (java.text.ParseException e1) 
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 
    request.addProperty("dtFrom",dateObj);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;
    try 
    {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        if (envelope.getResponse() != null) 
        {
            if (envelope.bodyIn instanceof SoapFault) 
            {
                String str = ((SoapFault) envelope.bodyIn).faultstring;
                Log.i("", str);
            } 
            else 
            {
                SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
                Log.d("WS", String.valueOf(resultsRequestSOAP));
            }    
        };
    } 
    catch (Exception e) 
    {
        Log.d("WS","sss");
    }
}

When i change web method(something with out date it work),I get response in log) But when is this way with date i just get catch ("sss" ) in log,i debug and find that it brake on: androidHttpTransport.call(SOAP_ACTION, envelope);

But i not find anything about that in log except catch that i set...

For me this looks like the dateObj which you want to give to the webservice can not be parsed, thats why the exception occurre at this line:

androidHttpTransport.call(SOAP_ACTION, envelope);

But as your formatter has this format:

SimpleDateFormat curFormater = new SimpleDateFormat("dd/mmm/yyyy"); 

Maybe the (three!)"mmm" are causing the error?? I am pretty sure this will produce something like eg "Feb" for February and so on.. (eg "11/Feb/2014"):

Try something like:

SimpleDateFormat curFormater = new SimpleDateFormat("dd/mm/yyyy"); 

or

SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 

Btw, to avoid localisation and interoperability issues, i often use DateTime objects accurately formatted to Strings for giving that objects over to WebService. Because many times i had problems by interoperability between eg .asmx Webservice and J2EE web service (eg the range of DateTime is not the same for J2EE and .NET and if it's a null/nil value you also run in troubles).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM