4

我正在使用 wcf 服务的 android 中构建我的第一个应用程序。我正在使用 ksoap2 解析响应。响应实际上是在 C# 中定义的对象数组。我按照这个非常有用的指南做了这个。现在我的问题是我需要使用一个 wcf 服务,该服务在 C# 中再次返回一个对象数组,但这次这些对象的一些属性是其他对象。所以我的问题是如何映射内部对象以便我可以解析它们的属性?

如果我不清楚,我需要解析这样的对象:

public class OutterObject {

     private InnerObject1 io1;
     private InnerObject2 io2;

}

希望我足够清楚

好的,这是我的简化代码,我不能只发布我认为有问题的部分

    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

这是抛出的异常: java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to connectionInitializer.Course

4

1 回答 1

3

这是一个对我有用的例子。
这是来自 Web 服务的响应消息类型

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

方法loginUser定义

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

UserResponse(外部)包含User属性:

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;
    }       
}
  }

User类(内)

 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;
}
   }

这很重要:确保为外部类和内部类注册密钥。

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

最后,你可以通过强制转换得到结果:

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

希望这有帮助!

于 2012-10-16T10:13:25.240 回答