Parsing complex soap response

2019-02-18 08:41发布

问题:

I'm building my first application in android which consumes a wcf service.I'm using ksoap2 to parse the response.The response is actually an array of objects defined in C#.I did this following this really helpful guide.Now my problem is that I need to consume a wcf service which returns again 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 the inner objects so that i can parse their properties?

In case I was unclear, I need to parse an object like this:

public class OutterObject {

     private InnerObject1 io1;
     private InnerObject2 io2;

}

Hope I was clear enough

Ok this is my simplified code i can't post all of it just the portion i think is at fault

    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 the thrown exception: java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to connectionInitializer.Course

回答1:

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

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

The method loginUser definition

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

Class UserResponse (Outter) contains 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 (Inner)

 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 outter class and 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!



标签: android ksoap