2013-03-20 36 views
-3

我是Android新手。解析soap web服務並製作列表視圖

我一直在使用谷歌搜索了很多,但我找不到一個首選的例子或教程。

基本上,我只是想解析一個在Soap .net中製作的web服務,並從web服務數據中創建一個列表視圖。

請向我介紹任何您可能知道的例子。我只想要一個解析soap web服務並在Android中進行列表視圖的例子。

+0

你嘗試過什麼嗎? – Swayam 2013-03-20 12:04:07

+0

我試過兄弟.... – user2190693 2013-03-20 12:04:34

+0

http://www.helloandroid.com/tutorials/using-ksoap2-android-and-parsing-output-data – Swayam 2013-03-20 12:05:13

回答

0

步驟1:使用KSOAP2庫調用Web服務並將數據解析爲對象數組。

MyObject是用戶自定義的非原語對象我在這裏使用並且myObjectArray是對象的陣列,其存儲數據)

public void getData() 
    { 
     try { 

      SoapObject request = new SoapObject(NAMESPACE, METHOD); 

      //request.addProperty("", ""); // incase you need to pass parameters to the web-service 

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


      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      SoapObject result = (SoapObject) envelope.getResponse(); 
      int childCount = result.getPropertyCount(); 
      SoapObject tempArray[] = new SoapObject[childCount]; 
      for (i = 0; i < childCount; i++) { 
      tempArray[i] = (SoapObject) result.getProperty(i); 

      myObjectArray[i] = new MyObject(
        Integer.parseInt(tempArray[i].getProperty(0).toString()), 
        ....... // get individual data members of each object using getProperty(index) 
        Integer.parseInt(tempArray[i].getProperty(n).toString())); 

     } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

步驟2:顯示陣列ListView中的對象。

ArrayAdapter<MyObject> adapter = new ArrayAdapter<MyObject>(this, 
        android.R.layout.simple_list_item_1, myObjectArray); 

mListView.setAdapter(adapter);