Parsing complex soap response

后端 未结 1 645
陌清茗
陌清茗 2021-01-06 11:28

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#

相关标签:
1条回答
  • 2021-01-06 11:56

    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!

    0 讨论(0)
提交回复
热议问题