Friday, March 11, 2011

Complex objects with Ksoap2

My brain is fried, I won't even try to post sample code. You can find it HERE
He has supplied a very straightforward tutorial, that is clear, easy to follow, and very understandable. Good job! One major problem, though, I had a problem similar to many others. Although I could send an object to my .net web service and call my web method, when my object arrived at the web service destination it was null. I've spent the last several hours fighting with this, and I've had zero luck until it struck me.
Android code snippet:

MyComplexObject myObject = new MyComplexObject();
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setName("myObject");
propertyInfo.setValue(myObject);

.Net code:
[WebMethod]
public bool MyMethod(MyComplexObject entity)
{
}

Are you seeing the problem? Neither did I, because parameter names aren't supposed to matter, it's the underlying type that matters. I'll spell it out. In my java code I called my object "myObject" in my .net code I called my parameter "entity." As soon as I changed my parameter names so that both in the java code and the .Net code my instance/parameter was named "entity" I suddenly got my object deserializing in my web server! I don't know if this will solve your problem or not. KSoap2 seems to be a beast, and frankly if I can make it work, that will be enough for me without having to understand it for a while, but give it a shot. You may find that you get some nice .Net web service love.
Now, off to find out why I'm getting an org.ksoap2.serialization.SoapPrimitive exception when I get my response back from the server.
Good luck!

No comments:

Post a Comment