Android ksoap Cannot serialize: 0.0
Par PlaceOweb le dimanche, novembre 22 2015, 21:44 - JAVA - Lien permanent
Problème de sérialisation avec Ksoap ? "Cannot serialize: 0.0"
Selon les ressources ci dessous, on peut rajouter la classe MarshalDouble :
package org.ksoap2.serialization; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; import java.io.IOException; public class MarshalDouble implements Marshal { public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo expected) throws IOException, XmlPullParserException { return Double.parseDouble(parser.nextText()); } public void writeInstance(XmlSerializer writer, Object obj) throws IOException { writer.text(obj.toString()); } public void register(SoapSerializationEnvelope cm) { cm.addMapping(cm.xsd, "double", Double.class, this); } }
Et l'utiliser lors de l'appel à notre WS :
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); soapEnvelope.implicitTypes = true; soapEnvelope.dotNet = true; MarshalDouble md = new MarshalDouble(); md.register(soapEnvelope );
Mais étant donné que nous avons déjà créé un MarshalFloat, qui traite les float, double et decimal, dans le package org.ksoap2.serialization au lieu d'ajouter MarshalDouble, on peut utiliser notre MarshalFloat :
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); soapEnvelope.implicitTypes = true; soapEnvelope.dotNet = true; MarshalFloat md = new MarshalFloat(); md.register(soapEnvelope);
Déclaration de notre MarshalFloat qui sérialise les types : float, double et decimal, :
package org.ksoap2.serialization; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; import java.io.IOException; public class MarshalFloat implements Marshal { public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo propertyInfo) throws IOException, XmlPullParserException { String stringValue = parser.nextText(); Object result; if (name.equals("float")) { result = new Float(stringValue); } else if (name.equals("double")) { result = new Double(stringValue); } else if (name.equals("decimal")) { result = new java.math.BigDecimal(stringValue); } else { throw new RuntimeException("float, double, or decimal expected"); } return result; } public void writeInstance(XmlSerializer writer, Object instance) throws IOException { writer.text(instance.toString()); } public void register(SoapSerializationEnvelope cm) { cm.addMapping(cm.xsd, "float", Float.class, this); cm.addMapping(cm.xsd, "double", Double.class, this); cm.addMapping(cm.xsd, "decimal", java.math.BigDecimal.class, this); } }
Ressources :
- simpligility/ksoap2-android
- ksoap2-android - lightweight, efficient SOAP on Android
ksoap2-android A lightweight and efficient SOAP library for the Android platform.kObjects.org : kSOAP 2- How to serialize double value using kSOAP in Android
- Implementing KSOAP Marshal Interface