Как получить объект сложного типа, используя ksoap2
В соответствии с этим руководством я разработал приложение для извлечения данных из веб-службы.
В веб-сервисе он получит некоторый список идентификаторов и вернет объект класса SingleAddWithThumbnail.
WSDL (JAX-WS)
<xs:element name="getAddCardResponse" type="tns:getAddCardResponse"/>
<xs:element name="getCardDetails" type="tns:getCardDetails"/>
<xs:element name="getCardDetailsResponse" type="tns:getCardDetailsResponse"/>
<xs:complexType name="getAddCard">
<xs:sequence>
<xs:element name="idList" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getAddCardResponse">
<xs:sequence>
<xs:element name="return" type="tns:singleAddWithThumbnail" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="singleAddWithThumbnail">
<xs:sequence>
<xs:element name="addID" type="xs:string" minOccurs="0"/>
<xs:element name="addTitle" type="xs:string" minOccurs="0"/>
<xs:element name="shopName" type="xs:string" minOccurs="0"/>
<xs:element name="shopNo" type="xs:string" minOccurs="0"/>
<xs:element name="thumbnailPic" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
Класс SingleAddWithThumbnail
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
public class SingleAddWithThumbnail implements KvmSerializable{
String thumbnailPic;
String addID;
String shopNo;
String shopName;
String addTitle;
public SingleAddWithThumbnail() {
}
public SingleAddWithThumbnail(String thumbnailPic, String addID,
String shopNo, String shopName,String addTitle) {
this.thumbnailPic = thumbnailPic;
this.addID = addID;
this.shopNo = shopNo;
this.shopName = shopName;
this.addTitle = addTitle;
}
public String getAddTitle() {
return addTitle;
}
public void setAddTitle(String addTitle) {
this.addTitle = addTitle;
}
public String getThumbnailPic() {
return thumbnailPic;
}
public void setThumbnailPic(String thumbnailPic) {
this.thumbnailPic = thumbnailPic;
}
public String getAddID() {
return addID;
}
public void setAddID(String addID) {
this.addID = addID;
}
public String getShopNo() {
return shopNo;
}
public void setShopNo(String shopNo) {
this.shopNo = shopNo;
}
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
@Override
public Object getProperty(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return thumbnailPic;
case 1:
return addID;
case 2:
return shopNo;
case 3:
return shopName;
case 4:
return addTitle;
}
return null;
}
@Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 5;
}
@Override
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
// TODO Auto-generated method stub
switch (index) {
case 0:
info.type = PropertyInfo.STRING_CLASS;
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
break;
case 3:
info.type = PropertyInfo.STRING_CLASS;
break;
case 4:
info.type = PropertyInfo.STRING_CLASS;
break;
default:break;
}
}
@Override
public void setProperty(int index, Object value) {
// TODO Auto-generated method stub
switch (index) {
case 0:
thumbnailPic = value.toString();
break;
case 1:
addID = value.toString();
break;
case 2:
shopNo = value.toString();
break;
case 3:
shopName = value.toString();
break;
case 4:
addTitle = value.toString();
break;
default:
break;
}
}
}
Класс деятельности
public class WebServiceObjectPassingActivity extends Activity {
ArrayList<String> idListOfDownalodedAdds = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_service_object_passing);
TextView tv = (TextView)findViewById(R.id.testId);
tv.setText("testing");
SingleAddWithThumbnail t;
TumbImageLoadingTask task = new TumbImageLoadingTask();
task.execute(idListOfDownalodedAdds);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.web_service_object_passing, menu);
return true;
}
class TumbImageLoadingTask extends
AsyncTask<ArrayList<String>, Void, SingleAddWithThumbnail> {
private final String NAMESPACE = "http://lba.anjula.com/";
private final String URL ="http://112.135.136.3:8080/LBAServi/LBAServiWSDL";
private final String SOAP_ACTION = "http://lba.anjula.com/getAddCard";
private final String METHOD_NAME = "getAddCard";
@Override
protected SingleAddWithThumbnail doInBackground(ArrayList<String>... arg0 ) {
SingleAddWithThumbnail result = new SingleAddWithThumbnail();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo idListPropety = new PropertyInfo();
idListPropety.setName("idList");
idListPropety.setValue(arg0[0]);
idListPropety.setType(arg0[0].getClass());
request.addProperty(idListPropety);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
//envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, "SingleAddWithThumbnail",
SingleAddWithThumbnail.class);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.debug=true;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
result.setThumbnailPic(response.getProperty(0).toString());
result.setAddID(response.getProperty(1).toString());
result.setShopNo(response.getProperty(2).toString());
result.setShopName(response.getProperty(3).toString());
result.setAddTitle(response.getProperty(4).toString());
Log.i("WebService:::",
"Result Card Returning Id:" + result.getAddID());
return result;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(SingleAddWithThumbnail result) {
Log.i("WebServicePOSTEX:::",
"Result Card Returning Id:" + result.getAddID());
}
}
}
При запуске приложения выдает эти ошибки.
10-04 10:20:52.713: E/AndroidRuntime(2218): FATAL EXCEPTION: AsyncTask #1
10-04 10:20:52.713: E/AndroidRuntime(2218): java.lang.RuntimeException: An error occured while executing doInBackground()
10-04 10:20:52.713: E/AndroidRuntime(2218): at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-04 10:20:52.713: E/AndroidRuntime(2218): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-04 10:20:52.713: E/AndroidRuntime(2218): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-04 10:20:52.713: E/AndroidRuntime(2218): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-04 10:20:52.713: E/AndroidRuntime(2218): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-04 10:20:52.713: E/AndroidRuntime(2218): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-04 10:20:52.713: E/AndroidRuntime(2218): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-04 10:20:52.713: E/AndroidRuntime(2218): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-04 10:20:52.713: E/AndroidRuntime(2218): at java.lang.Thread.run(Thread.java:856)
10-04 10:20:52.713: E/AndroidRuntime(2218): Caused by: java.lang.RuntimeException: Cannot serialize: [Ljava.util.ArrayList;@41034f78
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:664)
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:649)
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:595)
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:573)
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:658)
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:555)
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:205)
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.transport.Transport.createRequestData(Transport.java:134)
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:144)
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
10-04 10:20:52.713: E/AndroidRuntime(2218): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
10-04 10:20:52.713: E/AndroidRuntime(2218): at com.example.webserviceobjectpassing.WebServiceObjectPassingActivity$TumbImageLoadingTask.doInBackground(WebServiceObjectPassingActivity.java:84)
10-04 10:20:52.713: E/AndroidRuntime(2218): at com.example.webserviceobjectpassing.WebServiceObjectPassingActivity$TumbImageLoadingTask.doInBackground(WebServiceObjectPassingActivity.java:1)
10-04 10:20:52.713: E/AndroidRuntime(2218): at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-04 10:20:52.713: E/AndroidRuntime(2218): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
Почему происходит эта ошибка и как я могу это исправить?