BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 03-06-2009, 06:45 PM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2008
Model: Pearl
PIN: N/A
Carrier: T-Mobile
Posts: 22
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Need some serious help with connecting to .NET webservice.

Please Login to Remove!

Alright, after messing around for weeks now, I've finally got a response from a web method that returns a string. However, now I'm trying to handle a response from a method that returns XML and I get all that anyType stuff.

From what I can tell looking around, I have to somehow map out the returned XML to some objects, but I can't figure out how. Here's what I have so far.

This is my calling function (although its no where near complete since I've been trying out different things):

Code:
    
public String FindStores(String PhoneNumber, String Latitude, String Longitude)
    {
        
         String rtnStr = "";
        
        String methodName = "FindStores";
        String soapAction = URN + methodName;
        
        SoapObject stamp = new SoapObject(URN, methodName);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        
        stamp.addProperty("PhoneNumber", PhoneNumber);
        stamp.addProperty("Latitude", Latitude);
        stamp.addProperty("Longitude", Longitude);
        
        envelope.setOutputSoapObject(stamp);
        envelope.bodyOut = stamp;
        envelope.dotNet = true;
        envelope.encodingStyle = SoapSerializationEnvelope.XSD;
        
        HttpTransport transport = new HttpTransport(wsAddr);
        //envelope.addMapping("com.mybbnamespace", "FindStoresObj", new FindStoresObj().getClass());
        //ClassMap classMap = new ClassMap();
        
                
        try {
            transport.debug = true;
            transport.call(soapAction, envelope);
        } catch (Exception e) {
            rtnStr += "call exception: " + e.toString();
        }
        
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            //envelope.parseBody(saxParser);
            saxParser.parse(envelope.bodyIn, new testSoapSaxClassHandler());
            //rtnStr = (envelope.getResponse()).toString();
            //FindStoresObj result = (FindStoresObj)envelope.getResponse();
            //rtnStr = result.toString();
            //SoapObject body = (SoapObject)envelope.bodyIn;
            //Vector stores = (Vector)body.getProperty(0);
            //Vector store = (Vector)store.;
            rtnStr = saxParser.toString();
        } catch (Exception e) {
            rtnStr += "response exception: " + e.toString();
        }
        
        return rtnStr;
        
    }
As you can see I've been trying just about everything to parse this incoming XML to no avail.

Now here's my FindStoresObj that I was trying to map to the response with no luck.

Code:
package net.mybbnamespace;

import org.kobjects.serialization.KvmSerializable;
//import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class FindStoresObj implements KvmSerializable {
    public String StoreName;
    public String Address1;
    public String Address2;
    public String City;
    public String State;
    public String Zip;
    public String BrandName;
    public String BrandIcon;
    public String Latitude;
    public String Longitude;
    public String Distance;
    
    public FindStoresObj(){}
    public FindStoresObj(String StoreName, String Address1, String Address2, String City, String State, String Zip, String BrandName, String BrandIcon, String Latitude, 
                        String Longitude, String Distance) 
    {
        this.StoreName = StoreName;
        this.Address1 = Address1;
        this.Address2 = Address2;
        this.City = City;
        this.State = State;
        this.Zip = Zip;
        this.BrandName = BrandName;
        this.BrandIcon = BrandIcon;
        this.Latitude = Latitude;
        this.Longitude = Longitude;
        this.Distance = Distance;
    }
    
    public Object getProperty (int index) {
        switch (index) {
            case 0: return StoreName;
            case 1: return Address1;
            case 2: return Address2;
            case 3: return City;
            case 4: return State;
            case 5: return Zip;
            case 6: return BrandName;
            case 7: return BrandIcon;
            case 8: return Latitude;
            case 9: return Longitude;
            case 10: return Distance;
            default: throw new RuntimeException ();
        }
    }
    
    public int getPropertyCount() {
        return 11;
    }
    
    public void getPropertyInfo (int index, PropertyInfo info) {
        switch (index) {
            case 0: 
                info.name = "StoreName";
                info.type = String.class;
                break;
            case 1:
                info.name = "Address1";
                info.type = String.class;
                break;
            case 2:
                info.name = "Address2";
                info.type = String.class;
                break;
            case 3:
                info.name = "City";
                info.type = String.class;
                break;
            case 4:
                info.name = "State";
                info.type = String.class;
                break;
            case 5:
                info.name = "Zip";
                info.type = String.class;
                break;
            case 6:
                info.name = "BrandName";
                info.type = String.class;
                break;
            case 7:
                info.name = "BrandIcon";
                info.type = String.class;
                break;
            case 8:
                info.name = "Latitude";
                info.type = String.class;
                break;
            case 9:
                info.name = "Longitude";
                info.type = String.class;
                break;
            case 10:
                info.name = "Distance";
                info.type = String.class;
                break;
            default:
                throw new RuntimeException ();
        }
    }
    
    public void setProperty (int index, Object value) {
        switch (index) {
            case 0: StoreName = (String) value; break;
            case 1: Address1 = (String) value; break;
            case 2: Address2 = (String) value; break;
            case 3: City = (String) value; break;
            case 4: State = (String) value; break;
            case 5: Zip = (String) value; break;
            case 6: BrandName = (String) value; break;
            case 7: BrandIcon = (String) value; break;
            case 8: Latitude = (String) value; break;
            case 9: Longitude = (String) value; break;
            case 10: Distance = (String) value; break;
            default: throw new RuntimeException ();
        }
    }
}
For some reason my ksoap library didn't have the KvmSerializable class in it, so I found it online and put it in my project under the proper namespace. Here's that in case anyone knows how it should look.

Code:
package org.kobjects.serialization;
  
     import org.ksoap2.serialization.PropertyInfo;
     /** 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);
   
         /** returns 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 the value of the property
             */
          void setProperty (int index, Object value);
      
     
      
        /** Fills the given property info record */
         
      void getPropertyInfo (int index, PropertyInfo info);
     
        
  }
And last but not least, the XML that's getting returned from the .NET web service.

Code:
        <stores xmlns="">
          <store storeName="Safeway" Address1="8875 WASHINGTON ST" Address2="" City="Denver" State="CO" Zip="80229" BrandName="Safeway" BrandIcon="[imgurl]" Latitude="39.85771" Longitude="-104.9779" Distance="4.8 miles" />
          <store storeName="KING SOOPERS #0689" Address1="1120 US HIGHWAY 287" Address2="" City="Broomfield" State="CO" Zip="80020" BrandName="King Soopers" BrandIcon="[imgurl]" Latitude="39.9321394409" Longitude="-105.0907846561" Distance="3.1 miles" />
          <store storeName="Safeway CONVENIENCE" Address1="12702 LOWELL BLVD" Address2="" City="Broomfield" State="CO" Zip="80020" BrandName="Safeway" BrandIcon="[imgurl]" Latitude="39.9276928243" Longitude="-105.0343629384" Distance="1.9 miles" />
          <store storeName="King Soopers" Address1="9200 FEDERAL BLVD" Address2="" City="Denver" State="CO" Zip="80260" BrandName="King Soopers" BrandIcon="[imgurl]" Latitude="39.86384" Longitude="-105.0251" Distance="2.9 miles" />
          <store storeName="Albertson's" Address1="400 E 104TH AVE" Address2="" City="Denver" State="CO" Zip="80233" BrandName="Albertsons" BrandIcon="[imgurl]" Latitude="39.8850681867" Longitude="-104.9816740178" Distance="3.7 miles" />
          <store storeName="King Soopers" Address1="10396 N WASHINGTON ST" Address2="" City="Denver" State="CO" Zip="80229" BrandName="King Soopers" BrandIcon="[imgurl]" Latitude="39.8850232714" Longitude="-104.9776558593" Distance="3.9 miles" />
          <store storeName="Safeway CONVENIENCE STORE #123" Address1="9190 HURON ST" Address2="" City="Denver" State="CO" Zip="80260" BrandName="Safeway" BrandIcon="[imgurl]" Latitude="39.8633381554" Longitude="-104.9965655696" Distance="3.8 miles" />
          <store storeName="7-ELEVEN 27551" Address1="12920 LOWELL BLVD" Address2="" City="Broomfield" State="CO" Zip="80020" BrandName="7-Eleven" BrandIcon="[imgurl]" Latitude="39.9317082538" Longitude="-105.0343599356" Distance="2.2 miles" />
          <store storeName="King Soopers" Address1="7970 WADSWORTH BLVD" Address2="" City="Arvada" State="CO" Zip="80003" BrandName="King Soopers" BrandIcon="[imgurl]" Latitude="39.8414104967" Longitude="-105.0816176428" Distance="4.5 miles" />
          <store storeName="King Soopers" Address1="5195 W 120TH AVE" Address2="" City="Broomfield" State="CO" Zip="80020" BrandName="King Soopers" BrandIcon="[imgurl]" Latitude="39.9143619577" Longitude="-105.0528607331" Distance="0.9 miles" />
          <store storeName="7-ELEVEN 27238" Address1="8378 SHERIDAN BLVD" Address2="" City="Arvada" State="CO" Zip="80003" BrandName="7-Eleven" BrandIcon="[imgurl]" Latitude="39.8491449146" Longitude="-105.0530919321" Distance="3.6 miles" />
          <store storeName="Albertson's" Address1="9120 W 100TH AVE" Address2="" City="Broomfield" State="CO" Zip="80021" BrandName="Albertsons" BrandIcon="[imgurl]" Latitude="39.878214109" Longitude="-105.0996503829" Distance="3.2 miles" />
          <store storeName="King Soopers" Address1="9991 WADSWORTH PKWY" Address2="" City="Broomfield" State="CO" Zip="80021" BrandName="King Soopers" BrandIcon="[imgurl]" Latitude="39.87807038" Longitude="-105.0957189693" Distance="3 miles" />
          <store storeName="7-ELEVEN 32809" Address1="10128 WADSWORTH PKWY" Address2="" City="Broomfield" State="CO" Zip="80021" BrandName="7-Eleven" BrandIcon="[imgurl]" Latitude="39.8806485193" Longitude="-105.0960002876" Distance="3 miles" />
          <store storeName="Albertson's" Address1="8080 SHERIDAN BLVD" Address2="" City="Arvada" State="CO" Zip="80003" BrandName="Albertsons" BrandIcon="[imgurl]" Latitude="39.8429735498" Longitude="-105.053089562" Distance="4.1 miles" />
        </stores>
I've seen a lot of people asking this exact same question, so once I have everything working I'm planning on writing a step by step tutorial on how to fully access and use data from a .net web service once and for all. I'm really surprised nothing like this has been written yet.

Last edited by GasBot : 03-06-2009 at 06:47 PM.
Offline  
Old 03-06-2009, 06:48 PM   #2 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2008
Model: Pearl
PIN: N/A
Carrier: T-Mobile
Posts: 22
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also, obviously that XML is wrapped in a SOAP envelope when coming back, but I'm not sure if I have to account for that or if ksoap just pulls the response out of it automatically.
Offline  
Old 03-07-2009, 08:54 PM   #3 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2008
Model: Pearl
PIN: N/A
Carrier: T-Mobile
Posts: 22
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anyone? Are there any better BlackBerry development forums than this?
Offline  
Old 03-09-2009, 12:12 AM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2007
Location: Ernakulam, Kerala, India
Model: 8320
Carrier: Airtel
Posts: 65
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi bot

I think you can have an alternate way to handle it. I'm not sure about how the mapping can be done perfectly. I do think that If you could write a parser/SAX handler you can resolve the problem.

BTW what you do with this code:

Code:
saxParser.parse(envelope.bodyIn, new testSoapSaxClassHandler());
I think you can parse it inside the 'testSoapSaxClassHandler' why dont you try like that ? Also If you find the other way please do post in this forum.
__________________
Regards
Anand.
Offline  
Old 03-10-2009, 11:10 AM   #5 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2008
Model: Pearl
PIN: N/A
Carrier: T-Mobile
Posts: 22
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, but how would I go about parsing it? The XML should just be parsed into a DOM that I can traverse. Does anyone know of any tutorials that go in depth about exactly how to handle ksoap? I'm really shocked that something so basic as consuming web services is so complicated on this platform.
Offline  
Old 03-17-2009, 09:24 PM   #6 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2008
Model: Pearl
PIN: N/A
Carrier: T-Mobile
Posts: 22
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just going to bump this in case anyone can help. I'm still stuck with this stuff.
Offline  
Old 03-18-2009, 03:17 PM   #7 (permalink)
Thumbs Must Hurt
 
Join Date: May 2008
Model: 8700
PIN: N/A
Carrier: Telcel
Posts: 69
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

So, your problem is struggling gettin the info out of the response right?

Its funny, ya checked my post were i explained how the reponse is retrieved

BlackBerryForums.com : Your Number One BlackBerry Community - View Single Post - $25.00 for someone to show me how to connect to a .NET web service

After the call, only get the Response out of the envelope on to a SoapObject, ya don't need the KVMSerialization, i also tried that and never got it to work, this is done automatically after cast to SoapObject so you don't need it.

PHP Code:
// We'll get the whole XML reponse with all the information
responseBody = (SoapObjectsse.getResponse();
// Now, the reponse works as a Vector. On position 0 we have the information of the Reponse while in position 1 we get the info we requested. So we get rid of the information reponse and lets grab the info we requested
responseBody = (SoapObjectresponseBody.getProperty(1);
// And now, we have the n tables the WebService returned to us with the information, but still we can't access the info, so we'll get one of the tables separated. We'll use a very different variable for not to lose the whole info the webservice sended us. Once again, the tables are contained on a Vector. Lets grab the first one
table = (SoapObjectresponseBody.getProperty(0);
// Now we have the table, but still can't use the info, soooo, lets grab a row individually
tableRow = (SoapObjecttable.getProperty(0); 
try printing each SoapObject on the console after each cast and see how the XML is being parsed and worked over and over only leaving what you really need to work with.

In the end, you i'll get a vector which can be accesed either by using the column index or name
Offline  
Old 03-18-2009, 09:27 PM   #8 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2008
Model: Pearl
PIN: N/A
Carrier: T-Mobile
Posts: 22
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah see that didn't work for me. It errored when trying to get the responseBody.getProperty(1). It didn't appear that that was a valid object. Honestly man, I'm willing to pay you to get this working for me. I can send you my project as it stands now and if you could just get one function properly working it'd be awesome. Let me know if you're interested.
Offline  
Old 03-19-2009, 12:47 PM   #9 (permalink)
Thumbs Must Hurt
 
Join Date: May 2008
Model: 8700
PIN: N/A
Carrier: Telcel
Posts: 69
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, i can see you hadn't meedled a lot with the documentation of a SoapObject object. Each elemente parsed from XML you received from the webservice response is stored on a Vector as a Vector element.

getProperty is a method inherited from the Vector class, so if you are not getting data on the slot 1 change to 0 maybe you data is stored there.

What i showed there is a way to get the data, but its not always the right thing to do.

i'd love to help you, but right now i have my hands full with my own work.
Offline  
Old 03-22-2009, 10:13 AM   #10 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2008
Model: Pearl
PIN: N/A
Carrier: T-Mobile
Posts: 22
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by CJLopez View Post
Ok, i can see you hadn't meedled a lot with the documentation of a SoapObject object. Each elemente parsed from XML you received from the webservice response is stored on a Vector as a Vector element.

getProperty is a method inherited from the Vector class, so if you are not getting data on the slot 1 change to 0 maybe you data is stored there.

What i showed there is a way to get the data, but its not always the right thing to do.

i'd love to help you, but right now i have my hands full with my own work.
Actually I did change it to 0 and it stopped erroring, but I couldn't get anything out of that object either. Do you know a good way to determine what's in each property? The JDE debugger kind of sucks compared to other debuggers I'm used to.
Offline  
Old 03-22-2009, 07:51 PM   #11 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2008
Model: Pearl
PIN: N/A
Carrier: T-Mobile
Posts: 22
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was able to figure it out. I needed to rewrite my web service so that it used child nodes under the store node instead of using attributes. Turns out ksoap didn't like the attributes.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.