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

Ksoap2 Android adding Attributes to a simple property(Ksoap2 Android将属性添加到一个简单的属性)
本文介绍了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将属性添加到一个简单的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)
Automatically copy property values from one object to another of a different type but the same protocol (Objective-C)(自动将属性值从一个对象复制到另一个类型不同但协议相同的对象 (Objective-C))