使KvmSerializable对象请求SOAP服务

时间:2015-05-29 07:08:44

标签: java android soap ksoap2 android-ksoap2

我只想从 android 请求SOAP服务,我在KvmSerializable对象的帮助下传递数据。

当我想发送简单数据时,我会完美地使用此引用link

但是当我想将复杂数据发送到SOAP服务时,我不知道如何形成KvmSerializable对象,例如在案例2中。我得到了一些ref link但无法解决它。

案例1:

要发送的数据:

<UserDetails>
   <name>string</name>
   <reportitem>string</reportitem>
   <reportid>long</reportid>  
</UserDetails>

我写过的KvmSerializable类:

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

import java.util.Hashtable;

public class UserDetails implements KvmSerializable {

private String name;
private String reportitem;
private String reportid;

public static final int NAME = 0;
public static final int REPORT_ITEM = NAME + 1;
public static final int REPORT_ID = REPORT_ITEM + 1;
public static final int PARAM_COUNT = REPORT_ID + 1;

@Override
public Object getProperty(int param_pos) {

    switch (param_pos) {
        case NAME:
            return name;

        case REPORT_ITEM:
            return reportitem;

        case REPORT_ID:
            return reportid;
    }
    return null;
}

@Override
public int getPropertyCount() {
    return PARAM_COUNT;
}

@Override
public void setProperty(int param_pos, Object val) {

    switch (param_pos) {
        case NAME:
            name = Long.parseLong(val.toString());
            break;

        case REPORT_ITEM:
            reportitem = val.toString();
            break;

        case REPORT_ID:
            reportid = val.toString();
            break;
    }
}

@Override
public void getPropertyInfo(int param_pos, Hashtable hashtable, PropertyInfo propertyInfo) {

    switch (param_pos) {
        case NAME:
            propertyInfo.type = PropertyInfo.STRING_CLASS;
            propertyInfo.name = "name";
            break;

        case REPORT_ITEM:
            propertyInfo.type = PropertyInfo.STRING_CLASS;
            propertyInfo.name = "reportitem";
            break;

        case REPORT_ID:
            propertyInfo.type = PropertyInfo.LONG_CLASS;
            propertyInfo.name = "reportid";
            break;
    }
}
}

案例2:

要发送的数据:

<organization>
    <orgname>string</orgname>
    <address>sring</address>
    <users>
      <user>
         <name>string</name>
         <age>int</age>
         <Address>
            <addr1>string</addr1>
            <addr2>string</addr2>
         </Address>
      </user>
      <user>
         <name>string</name>
         <age>int</age>
         <Address>
            <addr1>string</addr1>
            <addr2>string</addr2>
         </Address>
      </user>
    </users>
</organization>

在这种情况下,我想通过KvmSerializable对象将此复杂数据发送到SOAP服务。任何人都可以帮助我们如何通过KvmSerializable ......?

谢谢。

1 个答案:

答案 0 :(得分:2)

我认为您必须创建对象:地址,用户,用户和组织都实现KvmSerializable。

首先创建对象Address,因为这是最基本的对象。您可以像在案例1中那样执行此操作。

然后将对象设为User。

public class User implements KvmSerializable{

// The names of the variables, like they are defined in the WSDL
private static final String NAME = "name";
private static final String AGE = "age";
private static final String ADDRESS = "Address";

// DATA FIELDS
private String name;
private int age;
private Address address;

// KSOAP2 parsing methods
public Object getProperty(int index) {
    switch (index) {
    case 0:
        return name;
    case 1:
        return age;
    case 2:
        return address;
    default:
        break;
    }
    return null;
}

public int getPropertyCount() {
    return 3;
}

public void setProperty(int index, Object value) {
    switch(index){
    case 0:
        name = value.toString();
        break;
    case 1:
        age = Integer.parseInt(value.toString());
        break;
    case 2:
        address = (Address)value;
        break;
    default:
        break;
    }
}

public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
    switch(index){
    case 0:
        info.name = NAME;
        info.type = PropertyInfo.STRING_CLASS;
        break;
    case 1:
        info.name = AGE;
        info.type = PropertyInfo.INTEGER_CLASS;
        break;
    case 2:
        info.name = ADDRESS;
        info.type = Adress.class;
        break;
    default:break;
    }
}

在此之后,创建一个Users对象

public class Users implements KvmSerializable{

// The names of the variables, like they are defined in the WSDL
private static final String USER = "user";

// DATA FIELDS
private ArrayList<User> users = new ArrayList<User>();

// GETTERS & SETTERS
public ArrayList<User> getUsers(){
    return this.users;
}
public void setUsers(ArrayList<User> users){
    this.users = users;
}

// KSOAP2 parsing methods
public Object getProperty(int index) {
    switch (index) {
    case 0:
        return users;
    default:
        break;
    }
    return null;
}

public int getPropertyCount() {
    return 1;
}

@Override
public void setProperty(int index, Object value) {
    if (value != null) {
        switch (index) {
        case 0:
            users.add((User)value);
            break;
        default:
            break;
        }
    }
}

public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
    switch (index) {
    case 0:
        info.type = User;
        info.name = USER;
        break;
    default:
        break;
    }
}

然后你应该像User一样创建Organization。

发送请求时,还应根据WSDL注册所有使用的对象。

envelope.addMapping(NAMESPACE, OBJECT_NAME, CLASS);

我没有尝试过这个解决方案,所以我不确定这是否完全正确。我根据我实现类似这样的方式制作了这个解决方案。

但我认为你应该试试这个,希望它能帮到你!