2013-03-11 60 views
0

我的情況如下: 我發送一個散列給它後,服務器上的php腳本得到一個JSON數組作爲響應。 JSON數組有兩種可能的形式。如果哈希無效,第一個就像{"status":0}。第二陣型,當散列是有效的,它會有約會 - >會像{"appointmentName":"Meeting","date":"2013-03-15","startTime":"10:00:00","endTime":"12:10:00","status":2}列出從json數組到動態列表視圖的內容

JSON數組可以用不止一個約會來填充。 見下圖:

{ 
    "appointmentName": "Proposal", 
    "date": "2013-11-11", 
    "startTime": "09:00:00", 
    "endTime": "13:00:00", 
    "status": 2 
} 
{ 
    "appointmentName": "Meeting", 
    "date": "2013-03-15", 
    "startTime": "10:00:00", 
    "endTime": "12:10:00", 
    "status": 2 
} 

我想列出這些約會在對Android應用程序內一個ListView。

我的方法處理的JSON陣:

public void handleActivationResult(String result) {  
    try {   
      JSONObject jsonObject = new JSONObject(new String(result));   
      String statusCode = jsonObject.getString("status"); 

      TextView mainView = (TextView) findViewById(R.id.textView1); 

      if (statusCode.equals("2")) { 
        //fill listview with appointments 
      } else if (statusCode.equals(0)) { 
       //empty string sent to server 
      } else if (statusCode.equals(5)) { 
       //hash doesnt match 
      } else if (statusCode.equals(6)) { 
       //correct hash, but no upcoming appointments 
      } else { 
       //unknown error 
      } 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

我的ListView的心不是什麼特別尚未:

<ListView 
    android:id="@+id/appointmentsList" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 
</ListView> 

應該怎樣蜜蜂,該handleActivationResult()方法將填充列表與約會?

+2

用於多個約會的JSON數組無效。檢查它使用http://jsonlint.com/ – 2013-03-11 09:10:41

+0

另外,要用JSON填充ListView,看看這篇文章:http://stackoverflow.com/a/8113337/782609 – kamituel 2013-03-11 09:12:02

+0

感謝您的答案。我嘗試了JSONAdapter,但現在的問題是,我不知道如何在我的JSONObject的情境中專門使用它。所以另一部分,我是否必須從我的PHP腳本中更改約會的輸出? – amoht 2013-03-11 09:28:42

回答

0

檢查json字符串包括"status":0,如果沒有將數據設置到適配器。

當你從服務器得到響應時,在創建json對象之前檢查字符串。它包含"status" 0"如果包含顯示錯誤字符串。因爲你沒有提到json中成功響應的狀態。

如果您在獲取"success":2之後再解析實際數據,解析狀態並相應採取行動。

+0

你好,它幾乎就是我想要做的。如果狀態爲2,則有我想要填寫的約會。對不起,我的語法x) – amoht 2013-03-11 11:05:27