如何从android(ksoap2)发送DateTime到.net webService?

时间:2012-09-05 10:58:04

标签: android web-services ksoap2 android-ksoap2

我有一个.net webservice,我必须发送一个param(dateTime类型),你可以看到:

 <startDateTime>dateTime</startDateTime>

在Android客户端,我使用kso​​ap2,我不知道,如何发送那种类型的数据? 请帮助设置此类型 - 下面的代码不起作用。

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

2 个答案:

答案 0 :(得分:4)

以下是我在应用程序中调用Web服务方法的方法。请注意我用于转换java日期的方法。您需要ISO日期格式。

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();
}

以下是返回实际字符​​串的方法:

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;
}

答案 1 :(得分:0)

在您的服务器中,客户端的回报是什么?

当我遇到这样的问题时,我会在Android上的Logcat中显示我正在发送的内容以及在服务器端显示的内容。

我的日期也有点问题(我正在使用kso​​ap2和webservices),我解决了在util.Date中发送日期的问题,然后我在项目中使用SimpleDateFormat和模式日期并将该字符串转换为我想要的是什么。

CYA, Bertan


这是我发送给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;
    }
}