Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 8
Registered: ‎03-23-2010
My Device: 8800
My Carrier: TMobile

Example of KSoap2 Wrapper classes

Does anyone have any examples of how to implement the KSoap2 wrappers which implement the KvmSerializable interface?  Also, how do I parse the XML result?

 

For example, here's my return XML for an action called GetCompanyList, which returns the id and name of a companies in my DB.  What classes would I need to create for this, and how would I parse the response?  I tried .getProperty(0) but no success.

 

 

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetCompanyListResponse xmlns="http://tempuri.org/ns">
<GetCompanyListResult>
<Company>
<ID>int</ID>
<Name>string</Name>
</Company>
<Company>
<ID>int</ID>
<Name>string</Name>
</Company>
</GetCompanyListResult>
</GetCompanyListResponse>
</soap:Body>
</soap:Envelope>
Highlighted
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Example of KSoap2 Wrapper classes

these links should prove helpful:

 

http://www.codeproject.com/KB/mobile/CallWebServiceFromkSOAP.aspx?msg=2784881

 

http://www.drdobbs.com/mobility/208800166;jsessionid=VXIPHLNUSYUVFQE1GHRSKHWATMY32JVN

 

http://www.blackberryforums.com/developer-forum/140930-blackberry-jde-ksoap-noobs.html

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Example of KSoap2 Wrapper classes

I'm not really sure what you're trying to do, but it's easy enough to build a SoapSerializableEnvelope using kSOAP2. For example:

 

 

      SoapObject s = new SoapObject (namespace, methodName);
      s.newInstance ();
      s.addProperty ("a", "5");
      s.addProperty ("b", "6");
      
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope 
                                           (SoapEnvelope.VER11);
      envelope.bodyOut = s;
      envelope.dotNet = true;
      envelope.implicitTypes = true;
      envelope.encodingStyle = "utf-8";
      envelope.enc = SoapSerializationEnvelope.ENC2001;
      envelope.xsd = SoapEnvelope.XSD;
      envelope.xsi = SoapEnvelope.XSI;

//Build connection
      HttpTransport transport = new HttpTransport
                                (IP + qualifiedPath);
      
      //Initiate Connection
      String response;
      
      try
      {
        transport.debug = true;
        
        transport.call (namespace + methodName, envelope);
        
        response = envelope.getResponse ().toString ();
        
        System.out.println (envelope.bodyIn);
      }
      catch (Exception e)
      {
        System.out.println (e);
      }

 

You can also build cutom properties:

 

 

      //Set up userID parameter
      PropertyInfo UIDInfo = new PropertyInfo ();
      UIDInfo.name = "userId";
      UIDInfo.type = PropertyInfo.INTEGER_CLASS;
      
      //Set up SID parameter
      PropertyInfo SIDInfo = new PropertyInfo ();
      SIDInfo.name = "SID";
      SIDInfo.type = PropertyInfo.STRING_CLASS;

...

s.addProperty ("UID", "0");
      s.addProperty ("SID", "");

 

 

There are other routes you can take involving SoapPrimitive objects for raw types, etc. The key thing to remember is that there's many different ways to approach problems such as this, which is one of the great things about programming. Still, the javadocs are probably one of your best resources for this library:

 

Overview (kSOAP2)

 

Hope that helps,

~Dom

 

P.S. - I didn't like that first like. I found it very unhelpful when I was first starting out with kSOAP.

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
New Contributor
Posts: 8
Registered: ‎03-23-2010
My Device: 8800
My Carrier: TMobile

Re: Example of KSoap2 Wrapper classes

[ Edited ]

I'm already able to call a simple webservice, however, I do not know how to parse the XML that is returned. Maybe I am misunderstanding, but I thought you had to create wrapper classes to parse the return XML if you do not manuallly want to parse it.  Is this correct? Do you have any examples of this? What is the purpose of the KvmSerializable interface?

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Example of KSoap2 Wrapper classes

Oh, I see -- You're trying to extract the information contained in the response. To be honest I've not had to do so. You should, however, receive a SoapObject from the request, stored in the BodyIn variable of the SoapSerializableEnvelope. If you set a breakpoint shortly after making the call to the web service you can explore the envelope and see exactly what is stored in that object. You may be able to extract the information directly.

 

*searches his screenshot repository*

 

Ah ha! I knew I had this lying around...

 

http://img.photobucket.com/albums/v151/dom1000ca/Eclipse_kSOAP_value.jpg

 

Hope that helps,

~Dom

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
New Contributor
Posts: 8
Registered: ‎03-23-2010
My Device: 8800
My Carrier: TMobile

Re: Example of KSoap2 Wrapper classes

DAquilina, what are you using the parse the XML response?

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Example of KSoap2 Wrapper classes

Well for something as simple as the "Adder" web service I posted above, the response is just an int, so you can do something as simple as:

 

 

response = envelope.getResponse ().toString ();
int i = Integer.parseInt (response);

With complex types it is likely more complex. I do remember seeing a few Threads either on this forum or elsewhere which discussed extracting this information, but I'm afraid I didn't save the links. If you can't find your answers here, Google will likely be of use.

 

 

Pertaining to my above post, you'll see that there's a path to the items stored in the response, which would be Company objects in your response. You might be able to do something like the following:

 

 

Company companyA = envelope.bodyIn.elementData [0];
Company companyB = envelope.bodyIn.elementData [1];

//etc...

 Bear in mind I've never tested this.

 

 

Hope that helps,

~Dom

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Contributor
Posts: 21
Registered: ‎08-25-2011
My Device: any
My Carrier: any

Re: Example of KSoap2 Wrapper classes

I am getting this response in getResponse(). how to retrieve data out of the below response.

 

GetCollectionDe tailsResponse{G etCollectionDe tailsResult=[CollectionDeta ils{Id=1; Name=Dasharath; Id1=2; Name1=Hadi; }, CollectionDetai ls{Id=2; Name=Dasharath2; Id1=22; Name1=Hadi2; }]; }