解析复杂的SOAP响应 [英] Parsing complex soap response

查看:156
本文介绍了解析复杂的SOAP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建在android的我的第一个应用程序中使用kso​​ap2解析response.The反应消耗一个WCF service.I'm实际上是用C#。我定义对象的数组,这样做下面这个真正有用的指南。现在我的问题是,我需要消费这再次返回对象的数组在C#中的WCF服务,但这次这些对象的某些属性等objects.So我的问题是我怎么能映射内的对象,这样我可以解析它们的属性?

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

好吧,这是我的简化$ C $词不能发布这一切只是我觉得部分是有过错的。

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

这就是抛出的异常:java.lang.ClassCastException:org.ksoap2.serialization.SoapObject不能转换为connectionInitializer.Course

and this is the thrown exception: java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to connectionInitializer.Course

推荐答案

这是为我工作的一个例子。
这是来自Web服务的响应消息类型

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>

方法 loginUser 定义

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

UserResponse (Outter)包含用户属性:

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

用户类(内蒙古)

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

这是很重要:请确保您注册键为outter类和内部类

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!

这篇关于解析复杂的SOAP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆