[英]getting soap object response from server.how to parse soap object in andorid
嗨这是我第一次使用肥皂,所以我有一些问题,我无法解析
soap object.I我正在使用( ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar )这个librery for soap.so这里我想为这五个字段制作单独的Bean类并将其存储到Arraylist或hashmap中请给我一些关于这个soap对象解析的建议。
catalogCategoryTree{category_id=1; parent_id=0; name=Root; position=1; level=0;
children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=3;
parent_id=1; name=Root Catalog; is_active=1; position=3; level=1;
children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=10;
parent_id=3; name=Furniture; is_active=1; position=10; level=2;
children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=23;
parent_id=10; name=Bedroom; is_active=1; position=1; level=3;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=22;
parent_id=10; name=Living Room; is_active=1; position=23; level=3;
children=ArrayOfCatalogCategoryEntities{}; }; }; };
item=catalogCategoryEntity{category_id=13; parent_id=3; name=Electronics; is_active=1;
position=13; level=2;
children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=8;
parent_id=13; name=Cell Phones; is_active=1; position=8; level=3;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=15;
parent_id=13; name=Computers; is_active=1; position=9; level=3;
children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=27;
parent_id=15; name=Build Your Own; is_active=1; position=1; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=28;
parent_id=15; name=Laptops; is_active=1; position=2; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=29;
parent_id=13; name=Hard Drives; is_active=1; position=3; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=30;
parent_id=13; name=Monitors; is_active=1; position=4; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=31;
parent_id=13; name=RAM / Memory; is_active=1; position=5; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=32;
parent_id=13; name=Cases; is_active=1; position=6; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=33;
parent_id=13; name=Processors; is_active=1; position=7; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=34;
parent_id=13; name=Peripherals; is_active=1; position=8; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; }; };
item=catalogCategoryEntity{category_id=12; parent_id=13; name=Cameras; is_active=1;
position=13; level=3;
children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=25;
parent_id=12; name=Accessories; is_active=1; position=25; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=26;
parent_id=12; name=Digital Cameras; is_active=1; position=26; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; }; }; }; };
item=catalogCategoryEntity{category_id=18; parent_id=3; name=Apparel; is_active=1;
position=14; level=2;
children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=4;
parent_id=18; name=Shirts; is_active=1; position=4; level=3;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=5;
parent_id=18; name=Shoes; is_active=1; position=5; level=3;
children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=16;
parent_id=5; name=Mens; is_active=1; position=16; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=17;
parent_id=5; name=Womens; is_active=1; position=17; level=4;
children=ArrayOfCatalogCategoryEntities{}; }; }; };
item=catalogCategoryEntity{category_id=19; parent_id=18; name=Hoodies; is_active=1;
position=19; level=3; children=ArrayOfCatalogCategoryEntities{}; };
item=catalogCategoryEntity{category_id=24; parent_id=18; name=Pants; is_active=0;
position=24; level=3; children=ArrayOfCatalogCategoryEntities{}; }; }; };
item=catalogCategoryEntity{category_id=20; parent_id=3; name=Household Items; is_active=0;
position=20; level=2
尝试下面的代码,androidHttpTransport是HttpTransportSE对象。 然后,您将获得正常的xml响应,并使用任何解析器(如sax或pull解析器)对其进行解析。
String response = androidHttpTransport.responseDump
我的完整代码如下,请尝试进行必要的更改。
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE("url");
androidHttpTransport.debug = true;
SoapObject soapObject = (SoapObject) envelope.bodyOut;
//Log.e("soap request->", "" + soapObject.toString());
androidHttpTransport.call(soap_Action, envelope);
String response = androidHttpTransport.responseDump;
Log.e("response dump->",""+response);
inputStream = IOUtils.toInputStream(response);
} catch (Exception e) {
e.printStackTrace();
}
try {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
HttpTransportSE androidHttpTransport = new HttpTransportSE("url");
SoapObject request = new SoapObject(NAMESPACE, "method name");
// parameter if any
request.addProperty("thing you want to pass as parameter", actual thing );
envelope.setOutputSoapObject(request);
androidHttpTransport.call(NAMESPACE+"/method name", envelope);
SoapObject soapObject= (SoapObject) envelope.getResponse();
yourBeanObject.set(soapObject.getPropertyAsString("PROPERTY NAME"));
}catch(Exception e){
}
你可以为每个对象做[在你的情况下3]。
Ksoap提供了一个名为getPropertyCount()的更有用的方法,它告诉你响应中没有对象。
kSOAP给予以下示例链接高度有用我建议您通过它。
https://code.google.com/p/ksoap2-android/wiki/Links
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.