0

我的情况如下:在向服务器发送哈希后,我从服务器上的 php 脚本中得到一个 JSON 数组作为响应。JSON-Array 有两种可能的形式。如果哈希无效,第一个就像{"status":0}. 第二种形式,当哈希有效时,其中会有约会 -> 就像{"appointmentName":"Meeting","date":"2013-03-15","startTime":"10:00:00","endTime":"12:10:00","status":2}.

JSON-Array 可以填充不止一个约会。见下文:

{
    "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-app 的 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() 方法将用约会填充列表?

4

1 回答 1

0

检查 json 字符串是否包含"status":0,如果没有将数据设置到适配器。

当您从服务器获得响应时,在创建 json 对象之前检查它包含的字符串"status" 0"是否包含显示错误字符串。因为您没有在 json 中提及成功响应的状态。

如果您在解析实际数据之前得到"success":2then ,请解析状态并采取相应措施。

于 2013-03-11T10:47:26.190 回答