2

我有一个 .net 网络服务,我必须在其中发送一个参数(日期时间类型),如您所见:

 <startDateTime>dateTime</startDateTime>

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

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

2 回答 2

5

这是我在应用程序中调用 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;
}
于 2012-10-17T12:11:58.063 回答
0

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

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

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

赛亚,贝尔坦


这是我发送到 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;
    }
}
于 2012-09-05T11:22:44.410 回答