In the previous article we talked about how to encapsulate the ability of Android to call WebService, adding the class of the previous chapter to our ability to communicate with webservice. Often we encounter webservice calls through objects to make actual interactive calls. So there's this chapter of building object passing.
First, let's take a look.
KSOAP2 This open source package provides an interface
/* copyright (c) 2003,2004, Stefan Haustein, Oberhausen, Rhld., Germany * * permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the software is * furnished to do so, subject to the following conditions: * * the above copyright notice and&Nbsp;this permission notice shall be included in * all copies or substantial portions of the software. * * the software IS PROVIDED "As is", without warranty of any kind, express or * implied, including but not limited to the warranties OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND Noninfringement. in no event shall the * authors or copyright HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * Liability, whether in an action of contract, tort or otherwise, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE Software or the use or other dealings * in the software. * * Contributor (s): john d. beatty, f. hunter, renaud tognelli * * */package org.ksoap2.serialization;import java.util.Hashtable;/** * Provides Get and set methods for properties. can be used to replace * reflection (to some extend) for "Serialization-aware" classes. Currently used * in kSOAP and the RMS based kobjects object repository */public interface kvmserializable { /** * returns the property at a specified index (for serialization) * * @param index * the specified index * @return the serialized property */ object getproperty (Int index); /** * @return the number of serializable properties */ int getpropertycount (); /** * sets the property with the given index to the given value. * * @param index * the index to be set * @param Value &nbsP;* the value of the property */ void setproperty (Int index, object value); /** * fills the given property info record. * * @param index * the index to be queried * @param properties * information about the (DE) serializer. not frequently used. * @param info * the return parameter, to be filled with information about the * property with the given index. */ void getpropertyinfo (int index, hashtable properties, propertyinfo info);}
The interface has such a sentence in Ksoap and the RMS based Kobjects object repository, the approximate meaning should be based on object storage when you can use him. (Of course, with the help of translation tools translated, what is wrong to understand, please contact me)
Then it means that we just need to implement this interface of the object to be passed to implement the object transmission!
So there are a lot of web-text implementation to teach you how to achieve! my example!
public test implements kvmserializable{ public string test1; public string test2; // returns the property at a specified index (for serialization) //returns a specific property by index (translation: Returns the property at the specified index (serialized)) @Override Public object getproperty (Int index) { // Depending on the interface annotation, the most straightforward operation is as follows switch (index) { &NBSP, ..... (return test1 , etc.) } } //return the number of serializable properties //number of returned properties (translation: Number of serializable properties returned) @Override public int getpropeRtycount () { // todo auto-generated method stub //Return Fixed quantity return 2; } //sets the property with the given index to the given value. // Assign parameters to PropertyInfo according to index (translation: The property is set to the given value with the given index.) ) @Override public void getpropertyinfo (int index , hashtable arg1, propertyinfo a) { //according to the interface annotation the most direct will be the following actions swtich (Index) { ... (set property value of a) } } // fills the given property info record. //assigns a value to the property of the corresponding index (translation: Populates a given attribute information record. ) @Override public void setproperty (Int index,  OBJECT ARG1) { switch (index) {     &NBSP, ..... (TEST1 = ARG1, etc.) } }}
There is no error, but when we have a lot of different classes to pass? What about hundreds of this class attributes?
Then we do not always need to do a repetitive operation. So why don't we write a generic conversion class!
So we have the following class when we don't consider the more complex, and certain data types:
import java.lang.reflect.field;import java.lang.reflect.invocationtargetexception;import java.lang.reflect.method;import java.lang.reflect.parameterizedtype;import java.lang.reflect.type; import java.util.arraylist;import java.util.hashtable;import java.util.list;import java.util.vector;import org.ksoap2.serialization.kvmserializable;import org.ksoap2.serialization.propertyinfo;import org.ksoap2.serialization.soapobject;/** * Object Transport base class * @author Liu Yalin * @e-mail [email protected] * */public abstract basekvmserializable implements kvmserializable{ /** ** capitalize the first letter **/ public static string fristuppercase (STRING STR) {return string.valueof (Str.charat (0)). ToUpperCase (). Concat ( Str.substring (1)); } //returns the property at a specified index (for serialization) //returns a specific property by index (translation: Returns the property at the specified index (serialized)) @Override public object getproperty (Int index) { //Now that we're going to return the property values for a particular index, why don't we just return the field[] fs = by reflecting the corresponding property? this.getclass (). Getdeclaredfields (); field f = Fs[index]; string name = f.getname (); name = fristuppercase (name); String getMethodName = "Get";if (F.gettype () == boolean.class | | f.gettype () == boolean.class) { getMethodName = "is";} getmethodname += name; MeThod getmethod;object val = null;try {getmethod = this.getclass (). GetMethod (getmethodname); getmethod.setaccessible (true); Val = getmethod.invoke (this);} catch (nosuchmethodexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} catch (illegalaccessexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} catch (illegalargumentexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} catch (invocationtargetexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} return val; } //return the number of serializable properties //number of returned properties (translation: Number of serializable properties returned) @Override public iNt getpropertycount () { // TODO auto-generated method stub //Return Fixed quantity return this.getclass (). Getdeclaredfields (). length; } //Sets the property with the given index to the given value. //assigns parameters to PropertyInfo according to index (translation: The property is set to the given value with the given index.) ) @Override public void getpropertyinfo (int index , hashtable arg1, propertyinfo a) { field[] fs = this.getclass (). GetDeclaredFields ( ); field f = fs[index]; String name = f.getname ()///main is set type and name other needs can continue to add A.type = gettypebyclass (f.getType ());a.name = name; } // fills the given property info record. //assigns a value to the property of the corresponding index (translation: Populates a given attribute information record. ) @Override public void setproperty (Int index,  OBJECT ARG1) { Field[] fs = This.getclass (). Getdeclaredfields (); field f = fs[index]; String name = f.getname (); name = fristuppercase (name); string setmethodname = "Set" + name; Method m;try { m = this.getclass (). GetDeclaredMethod (SetMethodName, f.gettype ()); m.setaccessible (true); m.invoke (this, ARG1); } catch (nosuchmethodexception e) { // TODO Auto-generated catch block &nbsP; e.printstacktrace ();} catch (illegalaccessexception e) { // TODO Auto-generated catch block E.printstacktrace ();} catch (illegalargumentexception e) { // TODO Auto-generated catch block e.printstacktrace ();} catch (invocationtargetexception e) { // TODO Auto-generated catch block e.printstacktrace ();} } /** ** Available by Category PropertyInfo specific categories ** actually, except for the uniform category, it's not too much to use in order to feel better. ** you see, the following definitions of these classes are known **public static final class object_class = new object (). GETclass (); ** public static final class string_class = "". GetClass (); **public static final class integer_ Class = new integer (0). GetClass (); **public static final Class long_class = new long (0). GetClass (); **public static final class boolean_class = new boolean (True). GetClass (); **public static final class vector_class = new java.util.vector (). GetClass (); **/ public class gettypebyclass (Class  CLS) {if (Cls.isassignablefrom (boolean.class) | | cls.isassignablefrom (Boolean.class)) {return propertyinfo.boolean_class;} else if (Cls.isassignablefrom (string.class)) {return propertyinfo.string_claSS;} else if (Cls.isassignablefrom (integer.class) | | cls.isassignablefrom (int.class) | | cls.isassignablefrom (byte.class) | | cls.isassignablefrom (Byte.class)) {return propertyinfo.integer_class;} else if (Cls.isassignablefrom (Vector.class)) {return propertyinfo.vector_class;} else if (Cls.isassignablefrom (long.class) | | cls.isassignablefrom (Long.class)) {return propertyinfo.long_class;} else {return propertyinfo.object_class;}}}
Of course, this class has basically been able to satisfy most of the calls of non-complex classes.
However, some classes of nested complex types can still report serialized errors, where we will not delve into them for the time being.
If you are interested, you can continue to know:
Why does he report serialization errors?
When we writeelement again.
private void Writeelement (XmlSerializer writer, object element, PropertyInfo type, Object marshal) throws Ioexception{if ( marshal = null) ((Marshal) Marshal). Writeinstance (writer, element); else if (element instanceof Soapobject) Writeobjectbody (writer, (Soapobject) Element), else if (element instanceof kvmserializable) Writeobjectbody (writer, ( kvmserializable) Element), else if (element instanceof vector) Writevectorbody (writer, (vector) element, Type.elementtype) Elsethrow New RuntimeException ("Cannot serialize:" + Element);}
It was obvious that he could not serialize when he had no marshal and not a type of soapobject kvmserializable vector! Nature will be the error! So, according to this, did we catch something?
There is one such addmapping method in the Soapserializationenvelope marshal
His explanation is
Defines a direct mapping from a namespace and name to a Java class (and vice versa)
Interested to be able to look into it.
All right! The base object build pass will be here!
Since there is serialization, how to parse the service-side data received by KSOAP2? Please expect
Next "Android Call WebService Series KSoap2 Object Parsing"
This article is from the "Arps branding" blog, please be sure to keep this source http://laoyin.blog.51cto.com/4885213/1674017
Android calls the WebService series object build Pass