如何使用 KvmSerializable 将 SoapObject 的响应映射到 MyObject?
我在 ksoap2-android 上阅读了 wiki。但还是做不到。
我在信封中添加了一张地图:
envelope.addMapping(NAMESPACE, "return", new GoodsObject().getClass());
但我得到一个例外:
java.lang.ClassCastException: org.ksoap2.serialization.SoapObject
on the string
GoodsObject resultsRequestSOAP = (GoodsObject) envelope.bodyIn
代码如下。我在这个问题上花了很多时间,但仍然没有想法(
Java 类GoodsObject:
package com.deaddy64.droid;
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
public class GoodsObject implements KvmSerializable {
/**
*
*/
public String ref;
public String DeletionMark;
public String Code;
public String Description;
private static final long serialVersionUID = -4872313975732374054L;
public void setref(String strref) {
this.ref = strref;
}
public String getref() {
return this.ref;
}
public void setDeletionMark(String strDeletionMark) {
this.DeletionMark = strDeletionMark;
}
public String getDeletionMark() {
return this.DeletionMark;
}
public void setCode(String strCode) {
this.Code = strCode;
}
public String getCode() {
return this.Code;
}
public void setDescription(String strDescription) {
this.Description = strDescription;
}
public String getDescription() {
return this.Description;
}
/* (non-Javadoc)
* @see org.ksoap2.serialization.KvmSerializable#getProperty(int)
*/
public Object getProperty(int index) {
switch (index) {
case 0:
return ref;
case 1:
return DeletionMark;
case 2:
return Code;
case 3:
return Description;
}
return null;
}
/* (non-Javadoc)
* @see org.ksoap2.serialization.KvmSerializable#getPropertyCount()
*/
public int getPropertyCount() {
// TODO Auto-generated method stub
return 4;
}
/* (non-Javadoc)
* @see org.ksoap2.serialization.KvmSerializable#setProperty(int, java.lang.Object)
*/
public void setProperty(int index, Object value) {
switch(index) {
case 0:
this.ref = value.toString();
break;
case 1:
this.DeletionMark = value.toString();
break;
case 2:
this.Code = value.toString();
break;
case 3:
this.Description = value.toString();
break;
default:
break;
}
}
public void getPropertyInfo(int index, Hashtable properties,
PropertyInfo info) {
switch (index) {
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Ref";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "DeletionMark";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Code";
break;
case 3:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Description";
break;
}
}
}
这是ResponseDump:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header/>
<soap:Body> <m:getReferenceResponse xmlns:m="com.deaddy64">
<m:return xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Ref xmlns="http://v8.1c.ru/8.1/data/enterprise/current-config">7f4fb970-6a62-11e1-9bd0-001fd027b6bb</Ref>
<DeletionMark xmlns="http://v8.1c.ru/8.1/data/enterprise/current-config">false</DeletionMark>
<Code xmlns="http://v8.1c.ru/8.1/data/enterprise/current-config">000000003</Code>
<Description xmlns="http://v8.1c.ru/8.1/data/enterprise/current-config">123</Description>
</m:return>
</m:getReferenceResponse></soap:Body>
</soap:Envelope>
在调试器中的信封.bodyIn是:
(org.ksoap2.serialization.SoapObject) getReferenceResponse{return=anyType{Ref=7f4fb970-6a62-11e1-9bd0-001fd027b6bb; DeletionMark=false; Code=000000003; Description=123; }; }
主要活动:
package com.deaddy64.droid;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Droid1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String NAMESPACE = "com.deaddy64";
String URL = "http://10.0.2.2/InfoBase/ws/ws1.1cws";
String SOAP_ACTION = "com.deaddy64#testService:getReference";
String METHOD_NAME = "getReference";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping("http://v8.1c.ru/8.1/data/enterprise/current-config", "CatalogObject.Номенклатура", new GoodsObject().getClass());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.debug = true;
Log.v("dd64", "xml");
GoodsObject resultsRequestSOAP;
try {
// List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
//headerList.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("username:password".getBytes())));
androidHttpTransport.call(SOAP_ACTION, envelope);
resultsRequestSOAP = (GoodsObject) envelope.getResponse();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("dd64",androidHttpTransport.requestDump);
Log.v("dd64", androidHttpTransport.responseDump);
}
});
}
}