Ksoap2 Android向简单属性添加属性

Ksoap2 Android向简单属性添加属性,android,properties,attributes,ksoap2,Android,Properties,Attributes,Ksoap2,我试图在SoapObject中创建一个新属性,该属性将包含简单的字符串属性,其属性如下: <Power Unit="kw">1500</Power> public class ValueSerializationEnvelope extends SoapSerializationEnvelope { public ValueSerializationEnvelope(int version) { super(version); }

我试图在SoapObject中创建一个新属性,该属性将包含简单的字符串属性,其属性如下:

<Power Unit="kw">1500</Power>
public class ValueSerializationEnvelope extends SoapSerializationEnvelope {

    public ValueSerializationEnvelope(int version) {
        super(version);
    }

    public static final String TAG = ValueSerializationEnvelope.class.getSimpleName();


    @Override
    public void writeObjectBody(XmlSerializer writer, KvmSerializable obj) throws IOException {
        if (obj instanceof TextSoapObject) {
            writer.text(((TextSoapObject) obj).getText());
        }
        super.writeObjectBody(writer, obj);
    }
}
我能达到的最佳效果如下:

<Power Unit="kw"><>1500</></Power>
final TextSoapObject costOfRepairs = new TextSoapObject(namespace, "CostOfRepairs");
        costOfRepairs.addAttribute("Currency", getCurrency());
        costOfRepairs.setText(getRepairCosts() + "");
        root.addSoapObject(costOfRepairs);
我正在使用:


android上的ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar。

您可以在soap对象中添加属性并传递任何想要的值

SoapObject request = new SoapObject(NAMESPACE, NAME);
request.addProperty("powerInKw", 1500);

类似于键值对。

您可以在soap对象中添加属性并传递所需的任何值

SoapObject request = new SoapObject(NAMESPACE, NAME);
request.addProperty("powerInKw", 1500);

就像一对键值。

好的,我已经解决了这个问题,下面是如何解决的:

我创建了一个名为TextSoapObject的新soap对象

public class TextSoapObject extends SoapObject {
    public static final String TAG = TextSoapObject.class.getSimpleName();

    public TextSoapObject(String namespace, String name) {
        super(namespace, name);
    }

    public String text;

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}
接下来,我覆盖了SoapSerialization信封,如下所示:

<Power Unit="kw">1500</Power>
public class ValueSerializationEnvelope extends SoapSerializationEnvelope {

    public ValueSerializationEnvelope(int version) {
        super(version);
    }

    public static final String TAG = ValueSerializationEnvelope.class.getSimpleName();


    @Override
    public void writeObjectBody(XmlSerializer writer, KvmSerializable obj) throws IOException {
        if (obj instanceof TextSoapObject) {
            writer.text(((TextSoapObject) obj).getText());
        }
        super.writeObjectBody(writer, obj);
    }
}
就这样

要使用此功能,请执行以下操作:

<Power Unit="kw"><>1500</></Power>
final TextSoapObject costOfRepairs = new TextSoapObject(namespace, "CostOfRepairs");
        costOfRepairs.addAttribute("Currency", getCurrency());
        costOfRepairs.setText(getRepairCosts() + "");
        root.addSoapObject(costOfRepairs);
编辑:

ksoap2库已认识到此问题,并在此处解决:


应该在下一个ksoap2版本中修复。

好的,我已经解决了这个问题,下面是如何解决的:

我创建了一个名为TextSoapObject的新soap对象

public class TextSoapObject extends SoapObject {
    public static final String TAG = TextSoapObject.class.getSimpleName();

    public TextSoapObject(String namespace, String name) {
        super(namespace, name);
    }

    public String text;

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}
接下来,我覆盖了SoapSerialization信封,如下所示:

<Power Unit="kw">1500</Power>
public class ValueSerializationEnvelope extends SoapSerializationEnvelope {

    public ValueSerializationEnvelope(int version) {
        super(version);
    }

    public static final String TAG = ValueSerializationEnvelope.class.getSimpleName();


    @Override
    public void writeObjectBody(XmlSerializer writer, KvmSerializable obj) throws IOException {
        if (obj instanceof TextSoapObject) {
            writer.text(((TextSoapObject) obj).getText());
        }
        super.writeObjectBody(writer, obj);
    }
}
就这样

要使用此功能,请执行以下操作:

<Power Unit="kw"><>1500</></Power>
final TextSoapObject costOfRepairs = new TextSoapObject(namespace, "CostOfRepairs");
        costOfRepairs.addAttribute("Currency", getCurrency());
        costOfRepairs.setText(getRepairCosts() + "");
        root.addSoapObject(costOfRepairs);
编辑:

ksoap2库已认识到此问题,并在此处解决:

应在下一个ksoap2版本中修复。请尝试以下代码

SoapObject soOriginDestInfo = new SoapObject(namespace_ns, "OriginDestinationInformation");
SoapPrimitive spDeptDtTm = new SoapPrimitive(namespace_ns, "DepartureDateTime", "asdadsad");
spDeptDtTm.addAttribute("Unit", "kw");
soOriginDestInfo.addProperty("DepartureDateTime", spDeptDtTm);
request.addSoapObject(soOriginDestInfo);
请尝试以下代码

SoapObject soOriginDestInfo = new SoapObject(namespace_ns, "OriginDestinationInformation");
SoapPrimitive spDeptDtTm = new SoapPrimitive(namespace_ns, "DepartureDateTime", "asdadsad");
spDeptDtTm.addAttribute("Unit", "kw");
soOriginDestInfo.addProperty("DepartureDateTime", spDeptDtTm);
request.addSoapObject(soOriginDestInfo);

我正在使用一些.NET web服务,在使用ksoap处理此xml时,需要将addAttribute添加到addProperty:

    <Element FirstAttribute="int" SecondAttribute="double" />

以下是我为传递此请求所做的工作(包括一些额外的帮助循环和双精度传递):

    String xml = null;
    final String NAMESPACE = "http://yournamespace.org/";
    final String SOAP_ACTION = "http://yournamespace.org/NameOfYourOperation";
    final String METHOD_NAME = "NameOfYourOperation";
    final String URL = "http://whereyourserviceis.com/Service.asmx";
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    for (int i=0; i < anArray.size(); i++)
    {
        Some_obj obj = anArray.get(i);
        SoapObject attributes = new SoapObject(NAMESPACE, "Element");
        int a = Integer.parseInt(obj.someIntString);
        attributes.addAttribute("FirstAttribute", a);
        Double v = Double.parseDouble(obj.someDoubleString);
        attributes.addAttribute("Value", v);
        request.addProperty("SecondAttribute", attributes);
        //request.addSoapObject(request);
        Log.d(TAG,"=======obj.someIntString======: " + a + "=======obj.someDoubleString========: " + v);
    }
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    MarshalFloat md = new MarshalFloat();
    md.register(envelope);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    envelope.implicitTypes = true;
    envelope.setAddAdornments(false);

    try
    {
        HttpTransportSE transport = new HttpTransportSE(URL, 60000);
        transport.debug = true;
        transport.call(SOAP_ACTION, envelope);
        xml = transport.responseDump.toString();
        Log.d(TAG, "getUpdates===============" + xml);
    }
    catch(SocketException ex)
    {
        Log.e("SocketException : " , "Error on getUpdates() " + ex.getMessage());
    }
    catch (Exception e)
    {
        Log.e("Exception : " , "Error on getUpdates() " + e.getMessage());
    }
stringxml=null;
最终字符串名称空间=”http://yournamespace.org/";
最后一个字符串SOAP_ACTION=”http://yournamespace.org/NameOfYourOperation";
最终字符串方法\u NAME=“NameOfYourOperation”;
最终字符串URL=”http://whereyourserviceis.com/Service.asmx";
SoapObject请求=新的SoapObject(名称空间、方法名称);
对于(int i=0;i

请注意MarshalFloat部分…如果您要传递一个双精度值,以便soap可以正确序列化,则会出现此部分。

我正在使用一些.NET web服务,并且在使用ksoap处理此xml时需要将addAttribute添加到addProperty:

    <Element FirstAttribute="int" SecondAttribute="double" />

以下是我为传递此请求所做的工作(包括一些额外的帮助循环和双精度传递):

    String xml = null;
    final String NAMESPACE = "http://yournamespace.org/";
    final String SOAP_ACTION = "http://yournamespace.org/NameOfYourOperation";
    final String METHOD_NAME = "NameOfYourOperation";
    final String URL = "http://whereyourserviceis.com/Service.asmx";
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    for (int i=0; i < anArray.size(); i++)
    {
        Some_obj obj = anArray.get(i);
        SoapObject attributes = new SoapObject(NAMESPACE, "Element");
        int a = Integer.parseInt(obj.someIntString);
        attributes.addAttribute("FirstAttribute", a);
        Double v = Double.parseDouble(obj.someDoubleString);
        attributes.addAttribute("Value", v);
        request.addProperty("SecondAttribute", attributes);
        //request.addSoapObject(request);
        Log.d(TAG,"=======obj.someIntString======: " + a + "=======obj.someDoubleString========: " + v);
    }
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    MarshalFloat md = new MarshalFloat();
    md.register(envelope);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    envelope.implicitTypes = true;
    envelope.setAddAdornments(false);

    try
    {
        HttpTransportSE transport = new HttpTransportSE(URL, 60000);
        transport.debug = true;
        transport.call(SOAP_ACTION, envelope);
        xml = transport.responseDump.toString();
        Log.d(TAG, "getUpdates===============" + xml);
    }
    catch(SocketException ex)
    {
        Log.e("SocketException : " , "Error on getUpdates() " + ex.getMessage());
    }
    catch (Exception e)
    {
        Log.e("Exception : " , "Error on getUpdates() " + e.getMessage());
    }
stringxml=null;
最终字符串名称空间=”http://yournamespace.org/";
最后一个字符串SOAP_ACTION=”http://yournamespace.org/NameOfYourOperation";
最终字符串方法\u NAME=“NameOfYourOperation”;
最终字符串URL=”http://whereyourserviceis.com/Service.asmx";
SoapObject请求=新的SoapObject(名称空间、方法名称);
对于(int i=0;i

请注意MarshalFloat部分……如果您要传入一个双精度值,以便soap可以正确序列化,则会出现此部分。

如果这样做,则无法向该属性添加属性。我希望能够将该单元添加到您的powerInKw中。如果我这样做,我就不能向该属性添加属性。我希望能够将该单元添加到您的powerInKw中。你写了什么