无法使用kso​​ap2解析复杂对象(实体)

时间:2012-10-10 17:33:18

标签: android web-services ksoap2

我尝试使用this tutorialdocument,但我无法从响应中获取实体。
这是我的用户实体http://pastebin.com/Exx5Fgt6
请求代码:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

DoubleMarshal marshal = new DoubleMarshal();
marshal.register(envelope);
PropertyInfo userInfo = new PropertyInfo();
userInfo.setName("user");
userInfo.setValue(user);
userInfo.setType(user.getClass());
request.addProperty(userInfo);

envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new   HttpTransportSE(URL);
androidHttpTransport.debug = true;
envelope.addMapping(NAMESPACE, "user", new User().getClass());
                androidHttpTransport.call(SOAP_ACTION, envelope);

Log.i(TAG, "dump Request: " + androidHttpTransport.requestDump);
Log.i(TAG, "dump Response: " + androidHttpTransport.responseDump);

SoapObject response = (SoapObject) envelope.bodyIn;
Log.d(TAG, "Property 0: " +     response.getProperty(0).toString());
Log.d(TAG, "Property 1: " + response.getProperty(1).toString());
Log.d(TAG, "Property 2: " + response.getProperty(2).toString());

// Try pasrsing entity
User userReponse = (User) envelope.bodyIn;
Log.d(TAG, "user name: " + userReponse.username);

响应xml:

  <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://axn12.com">
<SOAP-ENV:Body>
    <ns1:registerUserResponse xmlns:ns1="http://axn12.com">
        <code xsi:type="xsd:int">1</code>
        <desc xsi:type="xsd:string">Success. (Executed time: 0.10681s)</desc>
        <user xsi:type="tns:user">
            <user_id xsi:type="xsd:int">5195</user_id>
            <username xsi:type="xsd:string">xcvx423424c</username>
            <email xsi:type="xsd:string">424242dasdfsadf@gmail.com</email>
            <password xsi:type="xsd:string">e258314984050bb53a9309592c6f96ab</password>
            <salt xsi:type="xsd:string">PXN</salt>
            <id_card xsi:type="xsd:string">Array</id_card>
            <fullname xsi:type="xsd:string">o0</fullname>
            <birthdate xsi:nil="true" xsi:type="xsd:string"/>
            <gender xsi:nil="true" xsi:type="xsd:string"/>
            <address xsi:nil="true" xsi:type="xsd:string"/>
            <country xsi:type="xsd:string">233</country>
            <location xsi:type="xsd:string">0</location>
            <zipcode xsi:nil="true" xsi:type="xsd:string"/>
            <mobile xsi:nil="true" xsi:type="xsd:string"/>
            <is_active xsi:type="xsd:int">0</is_active>
            <is_lock xsi:type="xsd:int">0</is_lock>
            <active_token xsi:type="xsd:string">elPp</active_token>
            <created_date xsi:type="xsd:string">2012-10-11 00:28:36</created_date>
            <point xsi:type="xsd:double">300000</point>
            <gold xsi:type="xsd:double">0</gold>
            <level xsi:type="xsd:int">0</level>
            <level_point xsi:type="xsd:int">0</level_point>
        </user>
    </ns1:registerUserResponse>
</SOAP-ENV:Body>

双元帅:

public class DoubleMarshal implements Marshal {

public Object readInstance(XmlPullParser parser, String namespace, String name,
        PropertyInfo expected) throws IOException, XmlPullParserException {

    return Double.parseDouble(parser.nextText());
}

public void register(SoapSerializationEnvelope cm) {
    cm.addMapping(cm.xsd, "double", Double.class, this);

}

public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
    writer.text(obj.toString());
}

}

问题是我在投射实体用户时遇到错误:

 10-11 00:08:23.952: W/System.err(1420): java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to com.org.domains.User
 10-11 00:08:23.963: W/System.err(1420):    at com.org.ducminh.WebServiceActivty$1.run(WebServiceActivty.java:125)
 10-11 00:08:23.963: W/System.err(1420):    at java.lang.Thread.run(Thread.java:856)

我的问题在哪里?

1 个答案:

答案 0 :(得分:1)

  

请回答我的问题然后我可以接受。 - R4j

好吧,好吧:)

bodyIn包含整个回复。您必须将其设为SoapObject,然后将该属性User作为类User