2013-03-11 59 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