2012-03-19 99 views
2

我想創建一個SoapObject內一個新的屬性,它包含與屬性,對他們這樣簡單的字符串屬性:Ksoap2 Android的屬性添加到一個簡單的屬性

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

我甚至嘗試添加的一切作爲一個字符串,但編碼<>標記。

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

我使用:

ksoap2-Android的組裝2.6.0-JAR-與-dependencies.jar在Android上。

回答

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

下一頁我overrided 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版本中修復。

+0

所以現在這是固定的,你將如何使用您的代碼示例當前ksoap2發佈的addAttribute?你可以添加另一個編輯? – whyoz 2014-01-07 19:03:49

+0

爲我的情況想出來,如果你在開始和結束標籤之間沒有文本,你可能想要檢查它。重要的工作Donnie .. – whyoz 2014-01-07 23:22:06

+0

你可以在這裏下載更新版本的庫:https:/ /oss.sonatype.org/content/repositories/ksoap2-android-releases/com/google/code/ksoap2-android/ksoap2-android/3.6.2/ – 2016-10-28 22:27:52

0

您可以在您的肥皂對象中添加屬性並傳遞您想要的任何值。

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

就像一個關鍵值對。

+0

如果我這樣做,我不能屬性,該屬性添加。我希望能夠在你的情況powerInKw添加單位。 你所寫的內容輸出到 – DArkO 2012-03-19 09:12:02

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

我消耗一些.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()); 
    } 

注意MarshalFloat部分......如果你傳遞了一個double值,那麼肥皂就可以正確序列化了。