我正在尝试在SoapObject中创建一个新属性,该属性将包含带有属性的简单字符串属性,如下所示:
<Power Unit="kw">1500</Power>
这是我在下面的示例中使用的代码。
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
我能达到的最好成绩如下:
<Power Unit="kw"><>1500</></Power>
我甚至尝试将所有内容添加为字符串,但编码&lt;&gt;标签
<Power Unit"kw">1500</Power>
我正在使用:
Android上的ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar。
答案 0 :(得分:2)
好的,我已经解决了这个问题,方法如下:
我创建了一个名为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信封:
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);
}
}
就是这样。
要使用此功能,您需要执行以下操作:
final TextSoapObject costOfRepairs = new TextSoapObject(namespace, "CostOfRepairs");
costOfRepairs.addAttribute("Currency", getCurrency());
costOfRepairs.setText(getRepairCosts() + "");
root.addSoapObject(costOfRepairs);
修改强>
此问题已被ksoap2库识别并在此处解决:
http://code.google.com/p/ksoap2-android/issues/detail?id=112
应该在下一个ksoap2版本中修复。
答案 1 :(得分:2)
尝试以下代码
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);
答案 2 :(得分:0)
您可以在soap对象中添加属性并传递任何所需的值。
SoapObject request = new SoapObject(NAMESPACE, NAME);
request.addProperty("powerInKw", 1500);
就像一个键值对。
答案 3 :(得分:0)
我正在使用一些.NET Web服务,并且在为此xml使用ksoap时需要将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());
}
记下MarshalFloat部分......如果你传入一个double值,那么就可以了,所以soap可以正确序列化。