【问题标题】:Parsing ksoap2 response解析 ksoap2 响应
【发布时间】:2011-04-22 05:14:47
【问题描述】:

我使用 ksoap2 库从 android 客户端与 SOAP Web 服务进行通信。 ksoap 团队做得很好,但问题是,没有任何好的例子可以在不同方面正确使用它。例如,我得到以下数据的肥皂响应:

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; }; 
    ...
}

它是一个复杂的对象,或者说是 StatusSetting 对象的集合。 当我尝试获取 SoapObject 的属性时,它只有 1 个属性,所有数据都作为字符串。它也不能被解析为json。令人难以置信的是,没有人遇到过关于 android 越来越受欢迎的问题。知道是否有人解决了这个问题以及如何解决这个问题会很酷。 谢谢。

【问题讨论】:

    标签: java android soap web-services ksoap2


    【解决方案1】:

    例如您的回复:

    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; 
           }; 
       }; 
     }
    

    对于请求和解析,请这样做:

    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)+"\n"+
                            "Category: "+CATEGORY+"\n"+
                            "Category URL: "+CATEGORY_URL+"\n"+
                            "Category_Icon: "+CATEGORY_ICON+"\n"+
                            "Category_Count: "+CATEGORY_COUNT+"\n"+
                            "SuperTag: "+SUPERTAG+"\n"+
                            "Type: "+TYPE+"\n"+
                            "******************************"
                       );                   
                       stringBuilder.append("\n");
                   }
               }
    

    【讨论】:

    • 在上述情况下假设 countryName = "" 然后它将其值解析为 anyType.. 即。 String countryName = anyType.. 有什么解决方案吗?
    • @Rooban Ponraj 一个很好的例子,但是这段代码只得到第一组响应,比如每次只得到 Books 数据响应,但没有得到 Cameras 数据响应...谁能帮帮我。谢谢
    【解决方案2】:
    SoapObject countryDetails = (SoapObject)envelope.getResponse();
    System.out.println(countryDetails.toString());
    
    ArrayList list = new ArrayList(countryDetails.getPropertyCount());
    lv_arr = new String[countryDetails.getPropertyCount()];
    for (int i = 0; i < countryDetails.getPropertyCount(); i++) {
        Object property = countryDetails.getProperty(i);
        if (property instanceof SoapObject) {
            SoapObject countryObj = (SoapObject) property;
            String countryName = countryObj.getProperty("countryName").toString();
            list.add(countryName );
        }
    }
    

    希望它应该工作

    【讨论】:

    • 在上述情况下假设 countryName = "" 然后它将其值解析为 anyType.. 即。 String countryName = anyType.. 有什么解决方案吗?
    【解决方案3】:

    这是tutorial on how to work with array of complex objects with KSOAP。我通过无数小时的调试发现了。希望这能帮助

    【讨论】:

      【解决方案4】:

      我的项目它的工作。希望这能有所帮助。

              SoapObject requestx = new SoapObject(NAMESPACE, METHOD_NAME);
      
              SoapSerializationEnvelope envelopex = new SoapSerializationEnvelope(SoapEnvelope.VER11);
              envelopex.dotNet = true;
              envelopex.setOutputSoapObject(requestx);
              HttpTransportSE httpTransportx = new HttpTransportSE(URL);          
      
              try  {                    
                  httpTransportx.call(SOAP_ACTION, envelopex);
                  SoapObject responsex = (SoapObject)envelopex.getResponse(); // not envelopex.bodyIn;
      
                   int i=0;
                   int RCount=responsex.getPropertyCount();
                   int[] tbIDArray = new int[RCount+1];
                   int[] iMonthAarray = new int[RCount+1];
                   int[] iYearAarray = new int[RCount+1];
                   String[] sDetailAarray = new String[RCount+1];
      
                   for (i = 0; i < RCount; i++) {
                       Object property = responsex.getProperty(i);
                       if (property instanceof SoapObject) {
                           SoapObject info = (SoapObject) property;
                           String tbID = info.getProperty("tbID").toString();
                           String iMonth = info.getProperty("iMonth").toString();
                           String iYear = info.getProperty("iYear").toString();
                           String sDetail = info.getProperty("sDetail").toString();
      
                          tbIDArray[i] =Integer.valueOf(tbID);
                          iMonthAarray[i] =Integer.valueOf(iMonth);
                          iYearAarray[i] =Integer.valueOf(iYear);
                          sDetailAarray[i] =sDetail;
                       }//if (property instanceof SoapObject) {
                   }//for (i = 0; i < RCount; i++) {
      
      
              }  catch (Exception exception)   {
                  MsgBox1(exception.toString() , "Error");
              }
      

      【讨论】:

        【解决方案5】:

        JSON 格式很舒服。我编写了一个简单的代码来将 SOAP V1 响应转换为 JSON:

        public static String soapStrToJson(String input)
        {
            String output;
        
            output = input;
            output = output.replace("key=", "\"");
            output = output.replace("; value=", "\":");
            output = output.replace("; };",",");
            output = output.replace("item=anyType{","");
            output = output.replace("Map","");
            output = output.replace(",}","}");
            output = output.replace(", }"," }");
            output = output.replace("null","0");
        
            Pattern pattern = Pattern.compile(":(.*?),");
            Matcher matcher = pattern.matcher(output);
        
            List<String> wordsToReplace = new ArrayList();
            while (matcher.find())
            {
                String str = matcher.group(0);
                if(str.contains("{") || str.contains("["))
                    continue;
        
                str = str.replace(":", "");
                str = str.replace(",", "");
        
                if(str.matches("^-?\\d+$"))
                    continue;
        
                System.out.println("-->" + str);
                wordsToReplace.add(str);
            }
        
            for(String str : wordsToReplace)
            {
                output = output.replace(":"+str+",", ":\""+str+"\",");
            }
            return output;
        }
        

        并将此功能用作:

        parseCategoryJSON( new JSONObject( soapStrToJson( env.getResponse().toString() ) ) )
        

        请注意,我没有使用 env.bodyIn SoapObject。

        这可能不是所有响应的完美转换器,因此请根据您的要求修改“String.replace”和 Pattern-Matchers。

        【讨论】:

        • 如果此问题与链接问题重复,请将其作为重复问题关闭,而不是留下答案。如果不是重复的,请留下完整的答案,而不是仅链接的答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2015-06-30
        相关资源
        最近更新 更多