Ksoap2 Android将属性添加到一个简单的属性

时间:2023-03-07
本文介绍了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>

这是我在下面的示例中使用的代码.

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>

我什至尝试将所有内容添加为字符串,但它会编码 <> 标签.

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

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

我正在使用:

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

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

推荐答案

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

Ok i have solved this issue, here is how:

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

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

接下来我像这样覆盖了 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将属性添加到一个简单的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:为情节提要中的部分文本 UILabel 添加下划线属性 下一篇:什么是 AttributeSet,我该如何使用它?

相关文章

最新文章