KSOAP为节点添加Custome类型

时间:2013-08-27 09:23:50

标签: android web-services ksoap2 android-ksoap2

我正在使用Android ksoap2库来使用SOAP Web服务。

请求中的一个节点看起来像..

//some more nodes
.............
    <dateVals i:type="n0:dateTimeArray">
                <item i:type="d:dateTime">1988-09-17</item>
    </dateVals>
..........
//some more nodes

我正在尝试

SoapObject request = new SoapObject(Commons.NAMESPACE,
            Commons.METHOD_NAME_REGISTER);

SoapObject dateParent = new SoapObject("", "dateVals");
        PropertyInfo dobItem = new PropertyInfo();

        dobItem.setName("item");
        dobItem.setValue("1988-09-09");
        dobItem.setType("d:dateTime");

        dateParent.addProperty(dobItem);

        request.addProperty("dateVals", dateParent);

从服务器抛出错误,因为它不是预期的格式。

我做错了什么?

修改

完整请求,此处HashMap<String, String> values;来自其他类。

SoapObject request = new SoapObject(Commons.NAMESPACE,
                Commons.METHOD_NAME_REGISTER);

        request.addProperty("authUser", Commons.AUTH_USER);
        request.addProperty("authKey", Commons.AUTH_KEY);
        request.addProperty("storeName", Commons.STORE_NAME);
        request.addProperty("sms", "true");

        request.addProperty("firstName", values.get("firstName"));
        request.addProperty("lastName", values.get("lastName"));
        request.addProperty("sex", values.get("sex"));
        request.addProperty("language", values.get("language"));
        request.addProperty("email", values.get("email"));

        SoapObject dateParent = new SoapObject("", "dateVals");
        PropertyInfo dobItem = new PropertyInfo();

        dobItem.setName("item");
        dobItem.setValue(values.get("dateVals"));
        dobItem.setType("d:dateTime");

        dateParent.addProperty(dobItem);

        request.addProperty("dateVals", dateParent);

        request.addProperty("phoneNumber", values.get("phoneNumber"));
        request.addProperty("password", values.get("password"));

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);



        HttpTransportSE httpTransportSE = new HttpTransportSE(
                Commons.URL_NEW_MEMBER);
        httpTransportSE.debug = true;

        try {

            Commons.lastTouchTime = System.currentTimeMillis();
            httpTransportSE.call(
                    Commons.NAMESPACE.concat(Commons.METHOD_NAME_REGISTER),
                    envelope);

            SoapObject response = (SoapObject) envelope.bodyIn;

            responseCode = Integer.parseInt(response.getProperty("return")
                    .toString());

        } catch (Exception e) {
            responseCode = -1;
            e.printStackTrace();
        } 

1 个答案:

答案 0 :(得分:3)

//对于任何方法,都需要Namespace。所以它不应该是黑色的。所以我改变了。

SoapObject dateParent = new SoapObject(Commons.NAMESPACE, "dateVals");
    PropertyInfo dobItem = new PropertyInfo();

    dobItem.setName("item");
    dobItem.setValue(values.get("dateVals"));
    dobItem.setType(PropertyInfo.OBJECT_CLASS);
    or
     dobItem.setType(PropertyInfo.STRING_CLASS);
    dateParent.addProperty(dobItem);

//使用addSoapObject()将任何子SoapObject添加到主请求Object。

 request.addSoapObject(dateParent);

//在形成Envelop时添加这两行

envelope.dotNet = true;
envelope.implicitTypes = true;

希望这会对你有所帮助。 如果您将共享完整的soap请求(XML格式)

,将会更容易