Web Service That Returns An Array of Objects With KSOAP
2013-08-30 14:47
531 查看
In my previous post, I wrote about an example of passing complex
objects with KSOAP. In this post, I will write about returning
arrays of objects with KSOAP.
If you want to know how to write a method that returns an array
of complex objects, look at this code:
Where the function InvokeMethod is :
GetSoapObject() and GetEnvelope() are:
MakeCall() is :
The most important part for retrieving the actual array of objects (in this case Category objects) is the following:
NOTE: Do
not use VECTOR class type provided by KSOAP! It does not work.
I really copy-pasted this from Eclipse, but hope it helps. Please write if you need more help. I hope I saved you at least some nerves.
All the best.
objects with KSOAP. In this post, I will write about returning
arrays of objects with KSOAP.
If you want to know how to write a method that returns an array
of complex objects, look at this code:
public static Category[] GetAllCategories() { String MethodName = "GetAllCategories"; SoapObject response = InvokeMethod(URL,MethodName); return RetrieveFromSoap(response); }
Where the function InvokeMethod is :
public static SoapObject InvokeMethod(String URL,String MethodName) { SoapObject request = GetSoapObject(MethodName); SoapSerializationEnvelope envelope = GetEnvelope(request); return MakeCall(URL,envelope,NAMESPACE,MethodName); }
GetSoapObject() and GetEnvelope() are:
public static SoapObject GetSoapObject(String MethodName) { return new SoapObject(NAMESPACE,MethodName); } public static SoapSerializationEnvelope GetEnvelope(SoapObject Soap) { SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(Soap); return envelope; }
MakeCall() is :
/** * * @param URL - The complete URL where the web service resides * @param Envelope - The envelope to be passed * @param NAMESPACE - The web method namespace * @param METHOD_NAME - The method name * @return - SoapObject containing the resultset */ public static SoapObject MakeCall(String URL, SoapSerializationEnvelope Envelope, String NAMESPACE, String METHOD_NAME) { AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL); try { androidHttpTransport.call(NAMESPACE + METHOD_NAME, Envelope); SoapObject response = (SoapObject)Envelope.getResponse(); return response; } catch(Exception e) { e.printStackTrace(); } return null; }
The most important part for retrieving the actual array of objects (in this case Category objects) is the following:
/** * * @param soap - represents the entering Soap object * @return returns the list of categories extracted from the response */ public static Category[] RetrieveFromSoap(SoapObject soap) { Category[] categories = new Category[soap.getPropertyCount()]; for (int i = 0; i < categories.length; i++) { SoapObject pii = (SoapObject)soap.getProperty(i); Category category = new Category(); category.CategoryId = Integer.parseInt(pii.getProperty(0).toString()); category.Name = pii.getProperty(1).toString(); category.Description = pii.getProperty(2).toString(); categories[i] = category; } return categories; }
NOTE: Do
not use VECTOR class type provided by KSOAP! It does not work.
I really copy-pasted this from Eclipse, but hope it helps. Please write if you need more help. I hope I saved you at least some nerves.
All the best.
相关文章推荐
- Why do some structures end with an array of size 1?
- CareerCup Fill the array with product of all numbers except the number in that cell
- Given an array of integers, every element appears twice except for one. Find that single one.
- Consuming an External Web Service with Domino 6
- Ruby: Sorting an array of strings, in alphabetical order, that includes some arrays of strings
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array
- [LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times
- returns a number of second with no user activity (called in an OnTimer event of a TTimer component).
- 转载:Using JQuery with an ASP.NET webservice
- Update: example of webserver with webservice
- given an array of integers Find the index of values that satisfy A+B = C + D
- Condensing an array of hashes within that same array in Ruby
- Things that will impact concurrency & capacity behavior of WCF service (with simoultaneous client requests/connections)
- Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i
- An Efficient Formal Testing Approach for Web Service with TTCN-3
- How to sort an NSMutableArray with custom objects in it?
- Given an array of size n, find the majority element. The majority element is the element that appear
- How to use Jackson to deserialise an array of objects
- Programmatically add credentials to an ASMX Web Service that is password protected in C#
- pointer to ref of an array, and to the func that return a ref of an array