Parsing complex soap response

天涯浪子 提交于 2019-12-01 07:14:16

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!