序列化字典以使用KSOAP2发送

时间:2015-04-08 10:51:28

标签: java android wcf ksoap2 webservices-client

我在尝试将字典发送到.NET Web服务时遇到问题。

我想提出请求:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<SetEquipment2 xmlns="http://tempuri.org/">
  <dso xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:KeyValueOfstringanyType>
      <a:Key>EquipmentType</a:Key>
      <a:Value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">10301</a:Value>
    </a:KeyValueOfstringanyType>
    <a:KeyValueOfstringanyType>
      <a:Key>Model</a:Key>
      <a:Value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">10401</a:Value>
    </a:KeyValueOfstringanyType>
    <a:KeyValueOfstringanyType>
      <a:Key>RemoteEndpointAddress</a:Key>
      <a:Value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">192.168.0.185</a:Value>
    </a:KeyValueOfstringanyType>
    <a:KeyValueOfstringanyType>
      <a:Key>BuildingID</a:Key>
      <a:Value i:type="b:short" xmlns:b="http://www.w3.org/2001/XMLSchema">1</a:Value>
    </a:KeyValueOfstringanyType>
    <a:KeyValueOfstringanyType>
      <a:Key>RoomId</a:Key>
      <a:Value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">78257ad6372a4ad8aaf53a2f3944963e</a:Value>
    </a:KeyValueOfstringanyType>
    <a:KeyValueOfstringanyType>
      <a:Key>EquipmentId</a:Key>
      <a:Value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">190.245|186.178|036.139</a:Value>
    </a:KeyValueOfstringanyType>
    <a:KeyValueOfstringanyType>
      <a:Key>EquipmentNm</a:Key>
      <a:Value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">xxxxxxxx</a:Value>
    </a:KeyValueOfstringanyType>
  </dso>
  <strCredential>xxxxxxxxxxxxxxx</strCredential>
</SetEquipment2>

我制作代码:

HashMap<String, Object> hashmap = new HashMap<String, Object>();
hashmap.put("EquipmentType", params.EquipmentType);
hashmap.put("Model", params.Model);
hashmap.put("RemoteEndpointAddress", params.RemoteEndpointAddress);
hashmap.put("BuildingID", params.BuildingID);
hashmap.put("RoomId", params.RoomId);
hashmap.put("EquipmentId", params.EquipmentId);
hashmap.put("EquipmentNm", params.EquipmentNm);

request.addProperty("dso", hashmap);

request.addProperty("strCredential", RoomViewerApp.mCredential);
SoapSerializationEnvelope envelope = getSoapEnvelope(request);

(new MarshalHashMap()).register(envelope);
String url = getWebServiceUrl(WEB_SERVICE_NAME);

HttpTransportSE androidHttpTransport = new HttpTransportSE(url);

androidHttpTransport.debug = true;

androidHttpTransport.call(SOAP_ACTION + mMethodName, envelope);

Log.d("", androidHttpTransport.requestDump);

androidHttpTransport.requestDump如下:

v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><SetEquipment2 xmlns="http://tempuri.org/">
<dso i:type="i:http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:KeyValueOfstringanyType>
    <Key i:type="d:string">BuildingID</Key>
    <Value i:type="d:int">10</Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
    <Key i:type="d:string">RemoteEndpointAddress</Key>
    <Value   i:type="d:string">192.168.6.15</Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
    <Key i:type="d:string">Model</Key>
    <Value i:type="d:string">10401</Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
    <Key i:type="d:string">EquipmentId</Key>
    <Value i:type="d:string">d4deebfea78d5401</Value>                  
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
    <Key i:type="d:string">RoomId</Key>
    <Value i:type="d:string">18f85dafc6c54569bd5de032b6f1dc0d</Value>  
 </a:KeyValueOfstringanyType>
 <a:KeyValueOfstringanyType>
     <Key i:type="d:string">EquipmentNm</Key>
     <Value i:type="d:string">SHW-M480W</Value>
 </a:KeyValueOfstringanyType><a:KeyValueOfstringanyType>
      <Key i:type="d:string">EquipmentType</Key>
      <Value i:type="d:string">10301</Value>
  </a:KeyValueOfstringanyType>/dso>
<strCredential i:type="d:string">So8MeQjQy+ULelSrIPx/BJ4uqnqisMNF</strCredential></SetEquipment2>

MarshalHashMap代码是:

public class MarshalHashMap implements Marshal {
/** use then during registration */
public static final String NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance";
/** use then during registration */
public static final String NAME = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
/** CLDC does not support .class, so this helper is needed. */
public static final Class HASHTABLE_CLASS = new HashMap().getClass();
SoapSerializationEnvelope envelope;

@Override
public Object readInstance(XmlPullParser parser, String namespace, String name,
        PropertyInfo expected) throws IOException, XmlPullParserException {

    HashMap instance = new HashMap();
    String elementName = parser.getName();
    while (parser.nextTag() != XmlPullParser.END_TAG) {
        SoapObject item = new ItemSoapObject(instance);
        parser.require(XmlPullParser.START_TAG, null, "KeyValueOfstringanyType");
        parser.nextTag();
        Object key = envelope.read(parser, item, 0, null, null, PropertyInfo.OBJECT_TYPE);
        parser.nextTag();
        if (key != null) {
            item.setProperty(0, key);
        }
        Object value = envelope.read(parser, item, 1, null, null, PropertyInfo.OBJECT_TYPE);
        parser.nextTag();
        if (value != null) {
            item.setProperty(1, value);
        }
        parser.require(XmlPullParser.END_TAG, null, "KeyValueOfstringanyType");
    }
    parser.require(XmlPullParser.END_TAG, null, elementName);
    return instance;
}

@Override
public void register(SoapSerializationEnvelope cm) {
    envelope = cm;
    cm.addMapping(MarshalHashMap.NAMESPACE, MarshalHashMap.NAME, HASHTABLE_CLASS, this);        
}

@Override
public void writeInstance(XmlSerializer writer, Object instance)
        throws IOException  {
    HashMap h = (HashMap) instance;
    SoapObject item = new SoapObject(null, null);
    item.addProperty("Key", null);
    item.addProperty("Value", null);       

    Iterator iterator = h.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry mapEntry = (Map.Entry) iterator.next();
        writer.startTag("", "a:KeyValueOfstringanyType");
        item.setProperty(0, mapEntry.getKey());
        item.setProperty(1, mapEntry.getValue());
        envelope.writeObjectBodyWithAttributes(writer, item);
        writer.endTag("", "a:KeyValueOfstringanyType");
    }

}

 class ItemSoapObject extends SoapObject {
        HashMap h;
        int resolvedIndex = -1;
        ItemSoapObject(HashMap h) {
            super(null, null);
            this.h = h;
            addProperty("key", null);
            addProperty("value", null);
        }

        // 0 & 1 only valid
        public void setProperty(int index, Object value) {
            if (resolvedIndex == -1) {
                super.setProperty(index, value);
                resolvedIndex = index;
            } else {
                // already have a key or value
                Object resolved = resolvedIndex == 0 ? getProperty(0) : getProperty(1);
                if (index == 0) {
                    h.put(value, resolved);
                } else  {
                    h.put(resolved, value);
                }
            }
        }
    }

}

如何通过ksoap2进行字典参数webservice调用?

0 个答案:

没有答案