2012-12-12 159 views
0

我試圖將一個aspx webservice返回的數組打印到一個android textview上。我實際上可以通過以下android一個C#aspx代碼讀取文本視圖中的數據。但問題是,當我打印的格式輸出它:表達式的類型必須是數組類型,但它解析爲SoapObject

anyType{string="Shean";string="Ya";string="Hey";}而不是 Shean雅&嘿

所以我決定讀數組如下:

for (int i = 0; i < intPropertyCount; i++) { 
       SoapObject responseChild = (SoapObject) response.getProperty(i); 
       int lengthOfResponseChild = responseChild.getPropertyCount(); 
       for (int j = 0; j < lengthOfResponseChild; j++) { 
        //Error Highlight is as responseChild[j] 
        result.setText(responseChild[j].toString()); 

       } 
       //Used this line and prints output on a textView as anyType{string="Shean";string="Ya";string="Hey";} 
       //result.setText(responseChild.toString()); 
      } 

然而,有在

result.setText(responseChild[j].toString()); 

被突出顯示一個錯誤其中指出The type of the expression must be an array type but it resolved to SoapObject

我的完整的Android代碼如下:

package com.example.fp1_webservicedropdown; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Spinner; 
import android.widget.TextView; 
import org.ksoap2.*; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.*; 

public class MainActivity extends Activity { 
    TextView result; 
    Spinner spinnerC; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     spinnerC = (Spinner) findViewById(R.id.spinner1); 
     result = (TextView) findViewById(R.id.textView2); 

     final String NAMESPACE = "http://sample.com/"; 
     final String METHOD_NAME = "GetCustomerList"; 
     final String SOAP_ACTION = "http://sample.com/GetCustomerList"; 
     final String URL = "http://myLocalIP/HelloWorldNew/Service1.asmx"; 

     SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 

     SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
       SoapEnvelope.VER11); 
     soapEnvelope.dotNet = true; 
     soapEnvelope.setOutputSoapObject(Request); 
     AndroidHttpTransport aht = new AndroidHttpTransport(URL); 

     try { 

      aht.call(SOAP_ACTION, soapEnvelope); 
      SoapObject response = (SoapObject) soapEnvelope.bodyIn; 
      int intPropertyCount = response.getPropertyCount(); 

      for (int i = 0; i < intPropertyCount; i++) { 
       SoapObject responseChild = (SoapObject) response.getProperty(i); 
       int lengthOfResponseChild = responseChild.getPropertyCount(); 
       for (int j = 0; j < lengthOfResponseChild; j++) { 
        //Error Highlight is as responseChild[j] 
        result.setText(responseChild[j].toString()); 

       } 
       //Used this line and prints output on a textView as anyType{string="Shean";string="Ya";string="Hey";} 
       //result.setText(responseChild.toString()); 
      } 


     } catch (Exception e) { 

      e.printStackTrace(); 

     } 
    } 
} 

我的完整的asp web方法的代碼如下:

[WebMethod] 
    public string[] GetCustomerList() 
    { 
     //substitute code to actually populate the array with the dataset data 
     string[] personIds = { "Shean", "Ya", "Hey" }; 
     return personIds; 
    } 

回答

2

responseChild是SoapObject的一個實例,但您嘗試使用數組訪問它的語法。

如果你想遍歷在responseChild屬性,SoapObject有方法getProperty(int index)

利用這一點,你的循環將如下所示:

for (int j = 0; j < lengthOfResponseChild; j++) { 
    result.setText(responseChild.getProperty(j).toString()); 
} 
+1

編輯我的一個基本固定的答案。請記住,我不熟悉KSoap – ireddick

+0

ireddick怪胎救生員:))非常感謝!我不能自己想象它:)它的工作 – Kasanova

相關問題