2009-08-13 43 views
5

我使用KSOAP2調用從Android應用程序一個.NET Web服務,並從Web服務的響應是按以下格式如何在Android中解析此Web服務響應?

anyType{ 
UserName=anyType{}; 
Password=anyType{}; 
ApplicationCode=JOB; 
ActionType=Query; 
MessageParameters=anyType{Parameters=anyType{}; }; 
TableData=anyType{TableNo=167; 
      TableName=Job; 
     DataRows= 
     anyType{ 
     DataRow= 
      anyType{ 
      DataRowValues= 
      anyType{ 
       DataRowValue= 
       anyType{ 
        FieldNo=1; 
        FieldName=No.; 
        PrimaryKey=true; 
        FieldType=Code20; DataValue=DEERFIELD, 8 WP; 
         }; 
       DataRowValue= 
       anyType 
         { 
        FieldNo=3; 
        FieldName=Description; 
        PrimaryKey=false; 
        FieldType=Text50; 
        DataValue=Setting up Eight Work Areas; 
         }; 
      DataRowValue= 
       anyType 
         { 
        FieldNo=4; 
        FieldName=Description 2; 
        PrimaryKey=false; 
        FieldType=Text50; 
        DataValue=anyType{}; 
         }; 
       }; 
       }; 
      }; 
     }; 
    }; 
ResponseForRequest=GETTABLEDATA; 
CustomIdentifier=TestBB; 
Applications=anyType{}; 
Forms=anyType{}; 
Menu=anyType{}; 
} 

我不知道這個響應的格式,我不知道如何解析這個反應,以獲得特定的結果。任何人都知道它,請幫助我。

注意:爲了您的理解,我手動設置了此響應的格式。

回答

7

實際上這是一個已知的格式,如果你知道Java腳本。這種格式的數據實際上是JSON Object的和JSON Array的。我希望你使用KSOAP2 library。因此,這裏是你如何解析這個結果。

如:

private Bundle bundleResult=new Bundle(); 
private JSONObject JSONObj; 
private JSONArray JSONArr; 
Private SoapObject resultSOAP = (SoapObject) envelope.getResponse(); 
/* gets our result in JSON String */ 
private String ResultObject = resultSOAP.getProperty(0).toString(); 

if (ResultObject.startsWith("{")) { // if JSON string is an object 
    JSONObj = new JSONObject(ResultObject); 
    Iterator<String> itr = JSONObj.keys(); 
    while (itr.hasNext()) { 
     String Key = (String) itr.next(); 
     String Value = JSONObj.getString(Key); 
     bundleResult.putString(Key, Value); 
     // System.out.println(bundleResult.getString(Key)); 
    } 
} else if (ResultObject.startsWith("[")) { // if JSON string is an array 
    JSONArr = new JSONArray(ResultObject); 
    System.out.println("length" + JSONArr.length()); 
    for (int i = 0; i < JSONArr.length(); i++) { 
     JSONObj = (JSONObject) JSONArr.get(i); 
     bundleResult.putString(String.valueOf(i), JSONObj.toString()); 
     // System.out.println(bundleResult.getString(i)); 
    } 
} 

起初我有很多的麻煩,這類數據,但最後我得到了這一切working.From然後我一直在使用this.I希望這可以幫助您解決問題。

+0

如何從解析方法得到結果到我的return語句中? – blankon91 2012-06-19 01:47:16

+0

現在您將得到格式結果的格式爲 {key1:val1,key2:val2,......}它們存儲在一個包中,所以返回'bundleResult' – 1HaKr 2012-06-19 05:45:05

+0

好吧,謝謝:) – blankon91 2012-06-19 07:05:50

0

我不認識格式。我認爲你將不得不解析你自己的反應,

一系列的正則表達式似乎是最快的開始。

如:

String intput = ""; //your big response string 
List<Map<String,String>> rows = new ArrayList<Map<String,String>>(); 
String[] rowdata = input.matches("DataRowValue\=\r\s*anyType{[^}]*};"); 


for (String r : rowdata){ 
    Map<String, String> row = new HashMap<String, String>(); 
    String[] nvpairs = r.split(";"); 

    for (string pair : nvpairs) { 
     String[] s = pair.split("="); 
     row.push(s[0], s[1]); 
    } 

} 

應該讓你開始。 由於很多原因,您可能需要修改第一個正則表達式。 類似「(?< = DataRowValue = [^ {] )[^}]」可能更合適。 我會被誘惑訪問什麼,只有通過直接的東西,如

字符串username = input.match釣出來出現一次( 「(< =用戶名\ =)?[^;] *」)

3

例如您的迴應:

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

前提是SOAP響應是一個有效的JSON格式;接受的答案可能並不總是成功的,因爲響應字符串不是以「{」開頭,而是以「anyType」開頭。

在這種情況下,我總是收到關於「anyType」不是有效的JSON對象的錯誤。然後我繼續用IndexOf(「{」);然後這開始解析,儘管如果響應字符串不是有效的JSON格式,它會中斷。

這裏的問題是我的響應字符串中有未轉義的字符,這些字符不能很好地與JSON格式配合使用。

參照這樣的回答:Android KSoap2: how to get property name

這就是我成功地實現:

public Bundle getElementsFromSOAP(SoapObject so){ 
    Bundle resultBundle = new Bundle(); 
    String Key = null; 
    String Value = null; 
    int elementCount = so.getPropertyCount();     

    for(int i = 0;i<elementCount;i++){ 
     PropertyInfo pi = new PropertyInfo(); 
     SoapObject nestedSO = (SoapObject)so.getProperty(i); 

     int nestedElementCount = nestedSO.getPropertyCount(); 
     Log.i(tag, Integer.toString(nestedElementCount)); 

     for(int ii = 0;ii<nestedElementCount;ii++){ 
      nestedSO.getPropertyInfo(ii, pi); 
      resultBundle.putString(pi.name, pi.getValue().toString()); 
      //Log.i(tag,pi.getName() + " " + pii.getProperty(ii).toString()); 
      //Log.i(tag,pi.getName() + ": " + pi.getValue()); 

     } 
    } 

    return resultBundle; 

} 
+0

嗨,這是否解決了手頭的問題? – Solace 2014-06-18 19:14:06

+0

@Zarah我不確定你在問你發佈的問題。你能否提供這方面的更多細節。 – ramizmoh 2014-06-19 08:46:14

+0

感謝您的回覆。我使用了你的方法,但整個數據字符串作爲一個屬性從web服務返回(propertyCount爲1)。其次,我的響應字符串無法在聯機JSON解析器中驗證,所以我認爲我的數據不是有效的JSON格式。我在這裏發佈了一個[問題](http://stackoverflow.com/questions/24268924/weird-soap-response-is-it-json-how-to-parse-it)。那麼你能告訴我什麼是解析web服務響應的合適方法嗎? – Solace 2014-06-19 15:02:14

0

SoapObject響應=(SoapObject)envelope.getResponse();

  int cols = response.getPropertyCount(); 

      for (int i = 0; i < cols; i++) { 
       Object objectResponse = (Object) response.getProperty(i); 




       SoapObject r =(SoapObject) objectResponse; 

       FieldName=(String) r.getProperty("FieldName").toString(); 

       // Get the rest of your Properties by 
       // (String) r.getProperty("PropertyName").toString(); 

      }