2012-02-15 83 views
0

在我的情況下,我有一個soap響應,其中存儲了一個「ArrayOfArrayOfString」類型的值。如何將SOAP響應轉換爲多維數組?

它就像一個陣列A [4] [4]

A [0] [0] - > SERVICEID

A [0] [1] - >的ServiceName

A [0] [2] - > ServiceImageURL

A [0] [3] - > ServiceDecription

A [0] [4] - > ServiceIconURL

及其所有相同高達A [4] [4]。

我該如何在android中處理這種類型的響應?

代碼是一樣的東西:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);   

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 

     envelope.setOutputSoapObject(request); 

     HttpTransportSE transportSE = new HttpTransportSE(URL); 
     transportSE.debug = true; 

              Log.i("WebService", "msg:try_out"); 
    String[] columns = null; 

    ArrayList<String> rows = new ArrayList<String>(); 

    try 
    { 
                Log.i("WebService", "msg:try+in"); 
     transportSE.call(SOAP_ACTION, envelope);        
                Log.i("WebService", "msg:SoapObject"); 


     SoapObject response = (SoapObject)envelope.getResponse();   

                Log.i("WebService", "Response"); 

try 
      { 
       // WhaT SHOULD I USE HERE to convert it to 2D Array// 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
       Log.v("CATCH BLOCK", e.getMessage()); 
      } 
} 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      Log.i("WebService", "msg:Exception error"); 
      Log.i("WebSerivce", e.getMessage()); 
      return e.getMessage(); 
     } 

請幫我對此。

+0

這裏是肥皂代碼::: – Harpreet 2012-02-15 12:16:05

+0

主持人:www.xyz.com 內容類型:text/xml的; charset = utf-8 內容長度:長度 SOAPAction:「http://abc.net/webservices/LoadServices」 <?xml version =「1.0」encoding =「utf-8」?> HTTP/1.1 200 OK 內容-Type:text/xml; charset = utf-8 內容長度:長度 – Harpreet 2012-02-15 12:17:47

+0

Harpreet 2012-02-15 12:18:21

回答

3

這是一個完美的工作代碼來分析複雜的數據...

我只是這樣做了[0] [1-4],並根據我的SOAP響應根據烏拉圭回合的肥皂修改代碼響應。

SoapObject result = (SoapObject)enevlop.getResponse(); 

      String str = result.getProperty(0).toString(); 
      // add a for loop for ur code and iterate it according to ur soap response and get all the node using getProperty(i); 

      String str1 = lameParser(str); 

      textView.setText(""+str1); 

現在定義lameParser()方法: -

public String lameParser(String input){ 

    String sName=input.substring(input.indexOf("sName=")+6, input.indexOf(";", input.indexOf("sName="))); 

    int IGoals=Integer.valueOf(input.substring(input.indexOf("iGoals=")+7, input.indexOf(";", input.indexOf("iGoals=")))); 

    String sCountry=input.substring(input.indexOf("sCountry=")+9, input.indexOf(";", input.indexOf("sCountry="))); 

    String sFlag=input.substring(input.indexOf("sFlag=")+6, input.indexOf(";", input.indexOf("sFlag="))); 

    return sName+"\n"+Integer.toString(IGoals)+"\n"+sCountry+"\n"+sFlag; 
} 
+0

Hi Himanshu, 感謝您的回答。但是,無論如何我不能直接將結果存儲到二維數組中? – Harpreet 2012-02-15 13:43:28

+0

我試圖使用你的代碼。我有一個問題,每個值之前我有「string =」,所以它只是每次選擇A [0] [0]。以下是實際完整回覆的快照。 https://885427425446120200-a-1802744773732722657-s-sites.googlegroups.com/site/harryb87/scrnpng – Harpreet 2012-02-15 13:55:07

+0

請檢查快照。我得到的答案是「string = 922」,而我只需要「922」。這個答案對於我指定的每個字段都是重複的,因爲沒有指定不同的東西,而是:: input.indexOf(「string =」):( – Harpreet 2012-02-15 14:02:48

1

這裏的代碼來解析XML數據的多個子節點...

public static void parseBusinessObject(String input, Object output) throws NumberFormatException, IllegalArgumentException, IllegalAccessException, InstantiationException{ 

     Class theClass = output.getClass(); 
     Field[] fields = theClass.getDeclaredFields(); 

     for (int i = 0; i < fields.length; i++) { 
      Type type=fields[i].getType(); 
      fields[i].setAccessible(true); 

      //detect String 
      if (fields[i].getType().equals(String.class)) { 
       String tag = "s" + fields[i].getName() + "="; //"s" is for String in the above soap response example + field name for example Name = "sName" 
       if(input.contains(tag)){ 
        String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag))); 
        if(strValue.length()!=0){ 
         fields[i].set(output, strValue); 
        } 
       } 
      } 

      //detect int or Integer 
      if (type.equals(Integer.TYPE) || type.equals(Integer.class)) { 
       String tag = "i" + fields[i].getName() + "="; //"i" is for Integer or int in the above soap response example+ field name for example Goals = "iGoals" 
       if(input.contains(tag)){ 
        String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag))); 
        if(strValue.length()!=0){ 
         fields[i].setInt(output, Integer.valueOf(strValue)); 
        } 
       } 
      } 

      //detect float or Float 
      if (type.equals(Float.TYPE) || type.equals(Float.class)) { 
       String tag = "f" + fields[i].getName() + "="; 
       if(input.contains(tag)){ 
        String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag))); 
        if(strValue.length()!=0){ 
         fields[i].setFloat(output, Float.valueOf(strValue)); 
        } 
       } 
      } 
     } 

    } 

如果你會喜歡的帖子給我投票使遊客輕鬆找到它....

+0

感謝您的幫助。 – Harpreet 2012-02-16 10:13:37

+0

是你解決這個問題的答案嗎? – himanshu 2012-02-16 10:35:10

+0

我正在努力。 – Harpreet 2012-02-16 10:36:57

1
try 
{ 
     Log.i("WebService", "Try Block"); 
     transportSE.call(SOAP_ACTION, envelope);       
     Log.i("WebService", "msg:SoapObject"); 

     SoapObject response = (SoapObject)envelope.getResponse(); 
     Log.i("WebService", "Response on"); 
     int totalService = response.getPropertyCount(); 

     int i; 
     String str ; 
     String str1; 

     for (i = 0; i < totalService; i++) 
     { 
     str = response.getProperty(i).toString(); 
      Log.i("WebService", "ForLoop "+ Integer.toString(i)); 
     str1 = lameParser(str, i); 
      Log.i("WebService", "ForLoop: lameParser done"); 
      Log.i("WebService", "Value Stored:: "+ str1); 
     } 
     }            
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      Log.i("WebService", "msg:Exception error"); 
      Log.i("WebSerivce", e.getMessage()); 

     }     

    } 



    private String lameParser(String input, int I) 
    { 
     int i = I; 
     Log.i("WebService", "LameParse()"); 
    try 
    { 
    String SId = input.substring(input.indexOf("{string=")+8, input.indexOf(";", input.indexOf("{string="))); 
     String SName = input.substring(input.indexOf(" string=")+8, input.indexOf(";", input.indexOf(" string="))); 
     String SIurl = input.substring(input.indexOf("http"), input.indexOf(";", input.indexOf("http"))); 
     String SIcon = input.substring(input.indexOf("jpg; string=")+12, input.indexOf("; }", input.indexOf("jpg; string="))); 

     // String[][] arr = new String[x][y]; is already initialized as local var of class. 
     arr[i][0] = SId; 
     arr[i][1] = SName; 
     arr[i][2] = SIurl; 
     arr[i][3] = SIcon; 



     return SId + "\n" + SName + "\n" + SIurl + "\n" + SIcon + "\n" ; 

     } 
     catch (Exception e) 
     { 
      Log.i("WebService", "catch exception"); 
      Log.i("WebService", e.getMessage()); 
      return null; 
     } 
    } 
1

下面是我如何處理ArrayOfArrayOfString對象。

SoapObject result = (SoapObject)envelope.bodyIn; 
if (result.getPropertyCount() > 0) { 
    SoapObject Rows = (SoapObject)result.getProperty(0); 
    int nRows = Rows.getPropertyCount(); 
    for (int nRow=0; nRow<nRows; nRow++) { 
     SoapObject Cols = (SoapObject)Rows.getProperty(nRow); 
     int nCols = Cols.getPropertyCount(); 
     for (int nCol=0; nCol<nCols; nCol++) { 
      String sCol = Cols.getProperty(nCol).toString(); 

      // Process sCol with nRow and nCol as array indexes 
     } 
    } 
}