0
我嘗試使用this tutorial和document,但我無法從響應中獲取實體。
這是我的用戶實體:http://pastebin.com/Exx5Fgt6
請求代碼:無法使用ksoap2解析複雜對象(實體)
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">[email protected]</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)
哪裏是我的問題嗎?
我猜bodiIn包含整個響應。你有沒有嘗試獲得bodyIn作爲SoapObject,然後獲得屬性「user」作爲類User? – mihail
像魅力一樣工作。非常感謝你 :)。請回答我的問題,然後我可以接受它。 – R4j