Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

java - Parsing ksoap2 response

I use a ksoap2 lib for communicating from android client with SOAP web service. Great job was done by ksoap team, but the problem is, there is no any good example how to use it correct in different aspects. For instance I get in soap response following data:

anyType{
    StatusSetting=anyType{Id=1; Name=Til afskrivning; LocationId=1; Editable=true; Default=true; Transcribed=false; }; 
    StatusSetting=anyType{Id=2; Name=Afskrevet; LocationId=1; Editable=false; Default=false; Transcribed=true; }; 
    ...
}

It's a complex object, or rather a collection of StatusSetting objects. When I try to get a property of SoapObject it's only 1 property with all that data as a string. It can't be parsed as json too. Unbelievable that nobody met same problem regarding to popularity android is gaining. Would be very cool to know if somebody solved this issue and how. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For example your Response:

anyType
{
  FOO_DEALS=anyType
  {
       CATEGORY_LIST=anyType
       {
         CATEGORY=Books; 
         CATEGORY_URL=books_chennai; 
         CATEGORY_ICON=http://deals.foo.com/common/images/books.png; 
         CATEGORY_COUNT=1045; 
         TYPE=1; 
         SUPERTAG=Books; 
       };
       CATEGORY_LIST=anyType
       {
           CATEGORY=Cameras;
           CATEGORY_URL=cameras_chennai;
           CATEGORY_ICON=http://deals.foo.com/common/images/cameras.png; 
           CATEGORY_COUNT=152; 
           SUPERTAG=Cameras; 
           TYPE=1; 
       }; 
   }; 
 }

For requesting and parsing do like this:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
           // Add the input required by web service
           request.addProperty("city","chennai");
           request.addProperty("key","10000");

           SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
           envelope.setOutputSoapObject(request);

           // Make the soap call.
           androidHttpTransport.call(SOAP_ACTION, envelope);

           // Get the SoapResult from the envelope body.
           resultRequestSOAP = (SoapObject) envelope.bodyIn;


           System.out.println("********Response : "+resultRequestSOAP.toString());

           SoapObject root = (SoapObject) resultRequestSOAP.getProperty(0);
           SoapObject s_deals = (SoapObject) root.getProperty("FOO_DEALS");

           StringBuilder stringBuilder = new StringBuilder();

           System.out.println("********Count : "+ s_deals.getPropertyCount());

           for (int i = 0; i < s_deals.getPropertyCount(); i++) 
           {
               Object property = s_deals.getProperty(i);
               if (property instanceof SoapObject)
               {
                   SoapObject category_list = (SoapObject) property;
                   String CATEGORY = category_list.getProperty("CATEGORY").toString();
                   String CATEGORY_URL = category_list.getProperty("CATEGORY_URL").toString();
                   String CATEGORY_ICON = category_list.getProperty("CATEGORY_ICON").toString();
                   String CATEGORY_COUNT = category_list.getProperty("CATEGORY_COUNT").toString();
                   String SUPERTAG = category_list.getProperty("SUPERTAG").toString();
                   String TYPE = category_list.getProperty("TYPE").toString();
                   stringBuilder.append
                   (
                        "Row value of: " +(i+1)+"
"+
                        "Category: "+CATEGORY+"
"+
                        "Category URL: "+CATEGORY_URL+"
"+
                        "Category_Icon: "+CATEGORY_ICON+"
"+
                        "Category_Count: "+CATEGORY_COUNT+"
"+
                        "SuperTag: "+SUPERTAG+"
"+
                        "Type: "+TYPE+"
"+
                        "******************************"
                   );                   
                   stringBuilder.append("
");
               }
           }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...