Comprehensive Soap Response Analysis

I am creating my first android application that consumes a wcf service. I am using ksoap2 to parse the answer. The answer is actually an array of objects defined in C #. I did this after this very useful guide . Now my problem is that I need to use the wcf service, which again returns an array of objects in C #, but this time some properties of these objects are other objects. So my question is, how can I map internal objects so that I can analyze their properties?

If I do not understand, I need to analyze an object like this:

public class OutterObject { private InnerObject1 io1; private InnerObject2 io2; } 

I hope I was clear enough

Ok, this is my simplified code, I can’t publish it all, only the part that, in my opinion, is to blame

  SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); Request.addProperty("connString",args.get(0)); Request.addProperty("ClCode", args.get(1)); Request.addProperty("TeCode", args.get(2)); Request.addProperty("CourseDate", args.get(3)); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); envelope.addMapping(namespace, "SRV_WeekProgramm",newSRV_WeekProgramm().getClass()); envelope.addMapping(namespace, "Course", new Course().getClass()); envelope.addMapping(namespace, "StudentHours", new StudentHours().getClass()); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); Course response = (Course)envelope.bodyIn; //this is where it crashes 

and this is an abandoned exception: java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be passed to connectionInitializer.Course

+4
source share
1 answer

Here is an example that worked for me.
This is a type of response message from a web service.

  <message name="loginUserResponse"> <part name="code" type="xsd:int"/> <part name="desc" type="xsd:string"/> <part name="user" type="tns:user"/> </message> 

loginUser method loginUser

 <operation name="loginUser"> <documentation>Login user.</documentation> <input message="tns:loginUserRequest"/> <output message="tns:loginUserResponse"/> </operation> 

The UserResponse (Outter) class contains the User property:

 public class UserResponse implements KvmSerializable { public int code; public String desc; public User user; public Object getProperty(int arg0) { switch (arg0) { case 0: return code; case 1: return desc; case 2: return user; default: break; } return null; } public int getPropertyCount() { return 3; } public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { switch (index) { case 0: info.type = PropertyInfo.STRING_CLASS; info.name = "code"; break; case 1: info.type = PropertyInfo.STRING_CLASS; info.name = "desc"; break; case 2: info.type = User.class; info.name = "user"; break; default: break; } } public void setProperty(int index, Object value) { switch (index) { case 0: this.code = Integer.parseInt(value.toString()); break; case 1: this.desc = value.toString(); break; case 2: this.user = (User) value; default: break; } } } 

And the User class (Internal)

  public class User implements KvmSerializable { public int user_id; public String username; public String email; public String password; public User() { } public Object getProperty(int index) { switch (index) { case 0: return user_id; case 1: return username; case 2: return email; case 3: return password; default: return null; } } public int getPropertyCount() { return 4; } public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { switch (index) { case 0: info.type = PropertyInfo.INTEGER_CLASS; info.name = "user_id"; break; case 1: info.type = PropertyInfo.STRING_CLASS; info.name = "username"; break; case 2: info.type = PropertyInfo.STRING_CLASS; info.name = "email"; break; case 3: info.type = PropertyInfo.STRING_CLASS; info.name = "password"; break; default: break; } } public void setProperty(int index, Object value) { if(null == value) value = ""; switch (index) { case 0: user_id = Integer.parseInt(value.toString()); break; case 1: username = value.toString(); break; case 2: email = value.toString(); break; case 3: password = value.toString(); break; } } 

This is important : make sure you register the keys for both the outer class and the inner class.

  envelope.addMapping(NAMESPACE, "loginUserResponse", UserResponse.class); envelope.addMapping(NAMESPACE, "user", User.class); 

Finally, you can get the result by casting:

  HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVER_URL); //open the requisition androidHttpTransport.call(SOAP_ACTION, envelope);// call UserResponse response = (UserResponse)envelope.bodyIn; 

Hope this help!

+3
source

Source: https://habr.com/ru/post/1439992/


All Articles