2010-06-22 144 views
5

我使用kso​​ap2 lib從android客戶端與SOAP Web服務進行通信。 Ksoap團隊完成了很大的工作,但問題是,沒有任何好的例子可以在不同的方面使用它。比如我得到的SOAP響應以下數據:解析ksoap2響應

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的普及而遇到同樣的問題。知道是否有人解決了這個問題以及如何解決這個問題會非常酷。 謝謝。

回答

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

希望它應該工作

+0

在上面的例子中,假設countryName =「」那麼它解析它的值爲anyType。字符串countryName = anyType ..任何解決方案? – 2013-12-30 05:59:04

8

例如您的迴應:

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"); 
       } 
      } 
+0

在上面的例子中,假設countryName =「」那麼它將它的值解析爲anyType。字符串countryName = anyType ..任何解決方案? – 2013-12-30 05:59:46

+0

真棒例子。 – neteinstein 2014-02-21 01:34:33

+0

@Rooban Ponraj一個非常好的例子,但是這個代碼只獲得第一組響應,比如每次只獲取**圖書**數據響應,但沒有獲得**相機**數據響應......任何人都可以請幫助我。 謝謝 – 2017-01-23 05:21:37

0

我項目是工作。希望這一點。

 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"); 
     } 
0

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

,並使用此功能爲:那我不使用env.bodyIn SoapObject

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

注。

這可能不是所有響應的完美轉換器,因此請根據您的要求修改'String.replace'和Pattern-Matcher。

+0

如果此問題與鏈接問題重複,請將其作爲副本關閉,而不是留下對此效果的答案。如果它不是重複的,請留下一個完整的答案,而不是隻有鏈接的答案。 – josliber 2016-01-19 15:40:45