getting soap object response from server.how to parse soap object in andorid

心不动则不痛 提交于 2019-12-11 09:54:56

问题


Hi This is my first time to work on soap so I have some problem here I am unable to parse
soap object.I am using (ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar) this librery for soap.so here I want to make separate Bean class for these five fields and store it into Arraylist or hashmap.Please give me some suggestion for this soap object parsing.

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

回答1:


Try below code, androidHttpTransport is HttpTransportSE object. Then you will get the normal xml response and parse it using any parser like sax, or pull parser.

     String response = androidHttpTransport.responseDump

My full code is given below, try this out with necessary changes..

     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();

    }



回答2:


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){
}

YOU CAN DO IT FOR EACH OBJECT[in your case 3].
Ksoap provides one more useful method called getPropertyCount() which tells you the no of object in the response.
kSOAP GIVES THE FOLLOWING EXAMPLE LINKS HIGHLY USEFUL I SUGGEST YOU GO THROUGH IT.
https://code.google.com/p/ksoap2-android/wiki/Links



来源:https://stackoverflow.com/questions/25867178/getting-soap-object-response-from-server-how-to-parse-soap-object-in-andorid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!