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.