本文介绍了Ksoap2 Android的添加属性到一个简单的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个SoapObject内一个新的属性,它包含与属性,他们喜欢这种简单的字符串属性:

I am trying to create a new property inside a SoapObject that will contain simple string properties with attributes to them like this:

<Power Unit="kw">1500</Power>

这里是$ C $词现在用下面我的样本。

here is the code i am using for my samples below.

final SoapObject powerObject= new SoapObject(namespace, "Power");
        powerObject.addAttribute("Unit", getPowerUnit());

        PropertyInfo powerObjectProperty = new PropertyInfo();
        powerObjectProperty .setName("");
        powerObjectProperty .type = String.class;
        powerObjectProperty .setValue(getPower());
        powerObjectProperty .addProperty(powerObjectProperty);
        root.addSoapObject(powerObject); // this is my root for the hierarchy

这是我能达到的最好的是以下内容:

The best that i could reach is the following:

<Power Unit="kw"><>1500</></Power>

我甚至尝试添加一切作为一个字符串但恩codeS的&lt;>标记

i even tried to add everything as a string but that encodes the <> tags.

&ltPower Unit"kw"&gt1500&lt/Power&gt

我使用的:

ksoap2-机器人组装-2.6.0-JAR-与-dependencies.jar在Android上。

ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar on android.

推荐答案

好吧,我已经解决了这个问题,方法如下:

Ok i have solved this issue, here is how:

我已经创建了一个名为TextSoapObject新的SOAP对象

I have created a new soap object called TextSoapObject

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

接下来我overrided SoapSerialization信封是这样的:

Next i overrided SoapSerialization envelope like this:

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

这就是它。

要使用这一点,你会做以下内容:

To use this you would do the following:

final TextSoapObject costOfRepairs = new TextSoapObject(namespace, "CostOfRepairs");
        costOfRepairs.addAttribute("Currency", getCurrency());
        costOfRepairs.setText(getRepairCosts() + "");
        root.addSoapObject(costOfRepairs);

编辑:

此问题已被认定为ksoap2库,并在此问题:

This issue has been recognized for the ksoap2 library and addressed here:

的http:// code。 google.com/p/ksoap2-android/issues/detail?id=112

应固定在未来ksoap2发布。

Should be fixed in the next ksoap2 Release.

这篇关于Ksoap2 Android的添加属性到一个简单的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 12:48